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;
}
相关推荐
JKHaaa1 小时前
几种简单的排序算法(C语言)
c语言·算法·排序算法
草莓熊Lotso4 小时前
【数据结构初阶】--算法复杂度的深度解析
c语言·开发语言·数据结构·经验分享·笔记·其他·算法
KyollBM4 小时前
【CF】Day75——CF (Div. 2) B (数学 + 贪心) + CF 882 (Div. 2) C (01Trie | 区间最大异或和)
c语言·c++·算法
CodeOfCC5 小时前
c语言 封装跨平台线程头文件
linux·c语言·windows
momo卡6 小时前
MinGW-w64的安装详细步骤(c_c++的编译器gcc、g++的windows版,win10、win11真实可用)
c语言·c++·windows
超的小宝贝7 小时前
数据结构算法(C语言)
c语言·数据结构·算法
凤年徐9 小时前
【数据结构初阶】单链表
c语言·开发语言·数据结构·c++·经验分享·笔记·链表
hjyowl17 小时前
题解:AT_abc407_c [ABC407C] Security 2
c语言·开发语言·算法
old_power19 小时前
UCRT 和 MSVC 的区别(Windows 平台上 C/C++ 开发相关)
c语言·c++·windows
@老蝴19 小时前
C语言 — 编译和链接
c语言·开发语言