4.3 核心语言特性
c
复制代码
#include <stdio.h>
#include <stdlib.h>

// 链表节点结构体
struct Node {
    int data;
    struct Node* next;
};

// 创建新节点
struct Node* create_node(int data) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = data;
    node->next = NULL;
    return node;
}

int main() {
    struct Node* head = create_node(1);
    head->next = create_node(2);
    head->next->next = create_node(3);
    
    // 遍历链表
    for (struct Node* p = head; p != NULL; p = p->next) {
        printf("%d ", p->data);
    }
    printf("\n");
    return 0;
}

关键特性:

  • 指针:直接操作内存地址,是C语言最强大也最危险的特征
  • 结构体:将相关数据组合成复合类型
  • 预处理宏:通过#define#include等进行文本替换
  • 无垃圾回收:程序员负责手动管理内存(malloc/free
  • 静态类型:类型在编译时确定,但在早期版本中类型检查相对宽松