BTSB-面试题

面试笔试题

在32位系统里面,用C语言写一个程序,如何判断小端和大端

c 复制代码
#include <stdio.h>

// 判断系统字节序的函数
void checkEndianness() {
    unsigned int num = 1;
    char *ptr = (char*)&num;

    // 如果第一个字节存储的是最低有效字节,则是小端
    if (*ptr) {
        printf("Little endian\n");
    }
    // 否则是大端
    else {
        printf("Big endian\n");
    }
}

int main() {
    // 调用函数来检查字节序
    checkEndianness();
    return 0;
}

用C语言写一个链表逆置

c 复制代码
#include <stdio.h>
#include <stdlib.h>

// 定义链表节点结构体
struct Node {
    int data;
    struct Node* next;
};

// 函数声明
void insertAtEnd(struct Node** head_ref, int new_data);
void printList(struct Node* node);
void reverseList(struct Node** head_ref);

// 在链表末尾插入新节点
void insertAtEnd(struct Node** head_ref, int new_data) {
    // 为新节点分配内存
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    struct Node* last = *head_ref; // 用于遍历链表

    // 给新节点赋值
    new_node->data = new_data;
    new_node->next = NULL;

    // 如果链表为空,则新节点为头节点
    if (*head_ref == NULL) {
        *head_ref = new_node;
        return;
    }

    // 找到最后一个节点
    while (last->next != NULL)
        last = last->next;

    // 将新节点链接到最后一个节点
    last->next = new_node;
    return;
}

// 打印链表
void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
    printf("\n");
}

// 反转链表
void reverseList(struct Node** head_ref) {
    struct Node* prev = NULL;
    struct Node* current = *head_ref;
    struct Node* next = NULL;

    while (current != NULL) {
        // 保存当前节点的下一个节点
        next = current->next;
        // 将当前节点的指针反转
        current->next = prev;
        // 移动指针到下一对节点
        prev = current;
        current = next;
    }

    // 将头指针指向最后一个节点
    *head_ref = prev;
}

// 主函数
int main() {
    struct Node* head = NULL; // 初始化空链表

    // 在链表末尾插入节点
    insertAtEnd(&head, 1);
    insertAtEnd(&head, 2);
    insertAtEnd(&head, 3);
    insertAtEnd(&head, 4);
    insertAtEnd(&head, 5);

    printf("Original list: \n");
    printList(head);

    // 反转链表
    reverseList(&head);

    printf("Reversed list: \n");
    printList(head);

    return 0;
}

下面输出哪个选项能指向a[1] 这个元素?

对数组名 a 进行自增运算是非法的,因为数组名不是一个可修改的左值。在 C 语言中,数组名 a 表示数组的首地址,而数组的地址是一个常量,不能被修改。因此,a++ 这行代码会导致编译错误

c 复制代码
#include "stdio.h"
int main(void) {

    int a[5] = {1,2,3,4,5};
    int *p = a;
    a++;  //错误
    printf("%d\r\n",(a); // 错误

    printf("%d\r\n",*(a+1); // 正确
    return 0;
}

下面也是正确做法

c 复制代码
#include "stdio.h"
int main(void) {

    int a[5] = {1,2,3,4,5};
    int *p = a;
    p++;
    printf("%d\r\n",*p);

    return 0;
}
相关推荐
南棱笑笑生7 小时前
20251213给飞凌OK3588-C开发板适配Rockchip原厂的Buildroot【linux-6.1】系统时适配CTP触摸屏FT5X06
linux·c语言·开发语言·rockchip
南棱笑笑生9 小时前
20251213给飞凌OK3588-C开发板适配Rockchip原厂的Buildroot【linux-6.1】系统时适配type-C0
linux·c语言·开发语言·rockchip
小猪猪屁9 小时前
顺序表与链表:头插法与尾插法详解
c语言·数据结构·c++
历程里程碑9 小时前
C++ 5:模板初阶
c语言·开发语言·数据结构·c++·算法
R-G-B12 小时前
哈希表(hashtable),哈希理论,数组实现哈希结构 (C语言),散列理论 (拉链发、链接发),散列实现哈希结构,c++ 实现哈希
c语言·哈希算法·散列表·哈希表·数组实现哈希结构·散列实现哈希结构·c++ 实现哈希
历程里程碑12 小时前
C++ 6 :string类:高效处理字符串的秘密
c语言·开发语言·数据结构·c++·笔记·算法·排序算法
未来之窗软件服务12 小时前
幽冥大陆(四十八)P50酒店门锁SDK 苹果object c语言仙盟插件——东方仙盟筑基期
c语言·开发语言·酒店门锁·仙盟创梦ide·东方仙盟·东方仙盟sdk
东华万里13 小时前
第十四篇 操作符详讲
c语言·学习·大学生专区
李绍熹16 小时前
C语言数组与指针示例
c语言·开发语言
杨福瑞16 小时前
数据结构:队列
c语言·数据结构