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;
}
相关推荐
码农不惑5 小时前
2025.06.27-14.44 C语言开发:Onvif(二)
c语言·开发语言
凌肖战7 小时前
力扣网C语言编程题:在数组中查找目标值位置之二分查找法
c语言·算法·leetcode
BreezeJuvenile7 小时前
数据结构与算法分析课设:一元多项式求值
c语言·课程设计·数据结构与算法分析·一元多项式计算
悲伤小伞8 小时前
linux_git的使用
linux·c语言·c++·git
气质、小青年!9 小时前
【排序算法】
c语言·数据结构
智者知已应修善业9 小时前
【51单片机节日彩灯控制器设计】2022-6-11
c语言·经验分享·笔记·单片机·嵌入式硬件·51单片机
开-悟10 小时前
嵌入式编程-使用AI查找BUG的启发
c语言·人工智能·嵌入式硬件·bug
Natsume171015 小时前
嵌入式开发:GPIO、UART、SPI、I2C 驱动开发详解与实战案例
c语言·驱动开发·stm32·嵌入式硬件·mcu·架构·github
shaun200116 小时前
华为c编程规范
c语言
MeshddY16 小时前
(超详细)数据库项目初体验:使用C语言连接数据库完成短地址服务(本地运行版)
c语言·数据库·单片机