• C语言经典面试题目(十六)


    1、什么是C语言中的指针常量和指针变量?它们有什么区别?

    在C语言中,指针常量和指针变量是指针的两种不同类型。它们的区别在于指针的指向和指针本身是否可以被修改。

    1. 指针常量:指针指向的内存地址不可变,但指针本身的值(即指针变量)可以变化。一旦指针被初始化为某个地址,就无法修改它指向的地址,但可以改变指针的值为其他地址。

      int *const ptr; // ptr是一个指针常量,指向int类型的数据
      int x = 10;
      ptr = &x; // 合法,初始化ptr为x的地址
      *ptr = 20; // 合法,通过ptr修改x的值为20
      
      • 1
      • 2
      • 3
      • 4
    2. 指针变量:指针指向的内存地址和指针本身的值都可以变化。可以通过指针变量来修改指针指向的地址,也可以修改指针本身的值。

      int *ptr; // ptr是一个指针变量,指向int类型的数据
      int x = 10;
      ptr = &x; // 合法,初始化ptr为x的地址
      int y = 20;
      ptr = &y; // 合法,修改ptr的值为y的地址
      
      • 1
      • 2
      • 3
      • 4
      • 5

    2、如何在C语言中实现字符串的查找和替换操作?

    在C语言中,可以使用标准库函数来实现字符串的查找和替换操作。常用的函数包括:

    1. strstr:用于在字符串中查找子串的出现位置。
    2. strchr:用于在字符串中查找特定字符的出现位置。
    3. strrchr:用于在字符串中查找特定字符的最后一次出现位置。
    4. strtok:用于分割字符串为多个子串。
    5. strcspn:用于查找字符串中第一个不包含在指定字符集合中的字符的位置。

    以下是一个简单的示例,演示了如何实现字符串的查找和替换操作:

    #include 
    #include 
    
    int main() {
        char str[] = "hello world";
        char *ptr = strstr(str, "world");
        if (ptr != NULL) {
            printf("Substring found at position: %ld\n", ptr - str);
        } else {
            printf("Substring not found\n");
        }
    
        char newStr[] = "goodbye";
        strncpy(ptr, newStr, strlen(newStr)); // 替换字符串
        printf("Modified string: %s\n", str);
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3、C语言中的函数指针数组有什么作用?请举例说明。

    函数指针数组用于存储多个函数指针,使得可以根据需要动态选择调用哪个函数。常见的应用场景包括菜单选择、回调函数等。

    以下是一个示例,演示了如何使用函数指针数组实现菜单选择:

    #include 
    
    void func1() {
        printf("You selected option 1\n");
    }
    
    void func2() {
        printf("You selected option 2\n");
    }
    
    void func3() {
        printf("You selected option 3\n");
    }
    
    int main() {
        void (*menu[3])() = {func1, func2, func3}; // 函数指针数组
        int choice;
        printf("Enter your choice (1-3): ");
        scanf("%d", &choice);
        if (choice >= 1 && choice <= 3) {
            menu[choice - 1](); // 调用选择的函数
        } else {
            printf("Invalid choice\n");
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    4、C语言中的文件读写模式有哪些?请列举几个常用的文件读写模式。

    C语言中常用的文件读写模式包括:

    1. “r”:只读模式,文件必须存在,指针位于文件开头。
    2. “w”:写入模式,文件不存在时创建新文件,文件存在时清空文件内容,指针位于文件开头。
    3. “a”:追加模式,文件不存在时创建新文件,文件存在时保留原内容,在文件末尾添加新内容,指针位于文件末尾。
    4. “r+”:读写模式,文件必须存在,指针位于文件开头。
    5. “w+”:读写模式,文件不存在时创建新文件,文件存在时清空文件内容,指针位于文件开头。
    6. “a+”:读写模式,文件不存在时创建新文件,文件存在时保留原内容,在文件末尾添加新内容,指针位于文件末尾。

    5、如何在C语言中实现哈夫曼树数据结构?

    哈夫曼树是一种经典的数据结构,用于实现最优编码。在C语言中,可以通过二叉树的方式实现哈夫曼树。哈夫曼树的构建通常是通过构建哈夫曼树的算法来实现的,其中最常见的是哈夫曼编码算法。

    以下是一个简单的示例,演示了如何实现哈夫曼树的构建:

    #include 
    #include 
    
    typedef struct Node {
        int frequency;
        char data;
        struct Node *left;
        struct Node *right;
    } Node;
    
    Node *createNode(int frequency, char data) {
        Node *node = (Node *)malloc(sizeof(Node));
        node->frequency = frequency;
        node->data = data;
        node->left = NULL;
        node->right = NULL;
        return node;
    }
    
    void printTree(Node *root) {
        if (root != NULL) {
            printf("%c(%d) ", root->data, root->frequency);
            printTree(root->left);
            printTree(root->right);
        }
    }
    
    int main() {
        Node *node1 = createNode(5, 'a');
        Node *node2 = createNode(10, 'b');
        Node *node3 = createNode(15, 'c');
        Node *node4 = createNode
    
    (20, 'd');
        Node *node5 = createNode(25, 'e');
        Node *node6 = createNode(30, 'f');
    
        node5->left = node1;
        node5->right = node2;
        node6->left = node3;
        node6->right = node4;
    
        Node *root = createNode(node5->frequency + node6->frequency, '*');
        root->left = node5;
        root->right = node6;
    
        printf("Huffman tree: ");
        printTree(root);
        printf("\n");
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    在上面的示例中,创建了几个节点表示字符和频率,然后根据哈夫曼算法构建了哈夫曼树,并打印了该哈夫曼树的结构。

  • 相关阅读:
    对象转json,json添加转义符
    【整合】LSTM 时间序列预测任务 Time-Series-stock (模拟实现股票走势预测)
    开通一个幻兽帕鲁专用服务器多少钱?阿里云挺便宜
    使用 Curl 和 DomCrawler 下载抖音视频链接并存储到指定文件夹
    【Eigen】Chapter4 几何模块 Geometry
    代码随想录算法训练营第五十七天 | 392.判断子序列、115.不同的子序列
    探索iOS之CoreImage框架
    Ajax概述,封装以及联合模板引擎进行数据交互
    【双向数据绑定原理 vue2.0 与 vue3.0】
    OBU与千寻魔方的定位性能分析
  • 原文地址:https://blog.csdn.net/eason22/article/details/136759376