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;
}
相关推荐
Wx120不知道取啥名3 小时前
C语言跳表(Skip List)算法:数据世界的“时光穿梭机”
c语言·数据结构·算法·list·跳表算法
暗碳7 小时前
vscode c语言环境配置
c语言·ide·vscode
北冥有鱼被烹10 小时前
【代码模板】C语言如何修改文件权限?读写执行权限对应值是多少?(chmod(“./a.out“, 0741);bit 2 1 0表示 读 写 执行)
c语言
myloveasuka11 小时前
[Linux]进程与PCB的关系,进程的基本操作
linux·c语言·c++
Run_Teenage11 小时前
C语言 数据结构【双向链表】动态模拟实现
c语言·数据结构·链表
猫猫头有亿点炸12 小时前
C语言求3到100之间的素数
c语言
黑不拉几的小白兔12 小时前
第十五届蓝桥杯大赛软件赛省赛Python 大学 C 组题目试做(中)【本期题目:回文数组,挖矿】
c语言·python·蓝桥杯
梁下轻语的秋缘12 小时前
每日c/c++题 备战蓝桥杯(小球反弹)[运动分解求解,最大公约数gcd]
c语言·c++·学习·算法·数学建模·蓝桥杯
北冥有鱼被烹13 小时前
【代码模板】如何用FILE操作符打开文件?fopen、fclose
c语言
北冥有鱼被烹15 小时前
【问题记录】C语言一个程序bug定位记录?(定义指针数组忘记[])
c语言·bug