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;
}
相关推荐
不穿格子衬衫3 小时前
常用排序算法(下)
c语言·开发语言·数据结构·算法·排序算法·八大排序
aqua35357423583 小时前
蓝桥杯-财务管理
java·c语言·数据结构·算法
Word码4 小时前
数据结构:栈和队列
c语言·开发语言·数据结构·经验分享·笔记·算法
五花肉村长4 小时前
数据结构-队列
c语言·开发语言·数据结构·算法·visualstudio·编辑器
qinzechen4 小时前
分享几个做题网站------学习网------工具网;
java·c语言·c++·python·c#
yttandb6 小时前
重生到现代之从零开始的C语言生活》—— 内存的存储
c语言·开发语言·生活
结衣结衣.6 小时前
python中的函数介绍
java·c语言·开发语言·前端·笔记·python·学习
五味香7 小时前
C++学习,信号处理
android·c语言·开发语言·c++·学习·算法·信号处理
梓䈑7 小时前
【C语言】自定义类型:结构体
c语言·开发语言·windows
PYSpring7 小时前
数据结构-LRU缓存(C语言实现)
c语言·数据结构·缓存