You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言指针使用困惑:为何需要指针及实例疑问求助

为什么C语言需要用指针?先从你那个无意义的示例说起

你贴的代码里用指针给b赋值完全是多此一举——直接写b = a比绕指针简单得多。这种示例只会让人困惑,因为它根本没展示指针的核心用途。指针的价值在于直接操作内存地址,下面给你几个必须用指针的实际场景:


1. 修改函数外部的变量值

C语言的函数参数默认是「值传递」——函数里拿到的是参数的副本,修改副本不会影响外部变量。但如果传指针(变量的内存地址),函数就能直接操作外部变量的内存:

#include <stdio.h>

// 交换两个整数的函数,必须用指针实现
void swap(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}

int main() {
    int num1 = 5, num2 = 10;
    printf("交换前:num1=%d,num2=%d\n", num1, num2);
    swap(&num1, &num2); // 传入变量的地址
    printf("交换后:num1=%d,num2=%d\n", num1, num2);
    return 0;
}

如果不用指针,swap函数里修改的只是临时变量,外部的num1和num2根本不会变。


2. 动态分配内存

静态数组的大小必须是编译时确定的常量,但指针配合malloc/calloc可以在运行时根据需求分配内存(堆内存):

#include <stdio.h>
#include <stdlib.h>

int main() {
    int size;
    printf("输入数组大小:");
    scanf("%d", &size);
    
    // 动态分配size个int的内存
    int *arr = (int*)malloc(size * sizeof(int));
    if (arr == NULL) {
        printf("内存分配失败\n");
        return 1;
    }
    
    // 给动态数组赋值
    for (int i = 0; i < size; i++) {
        arr[i] = i + 1;
    }
    
    printf("动态数组内容:");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    
    free(arr); // 必须释放动态分配的内存
    return 0;
}

这种场景下,没有指针就没法访问堆上的动态内存。


3. 操作字符串和数组

C语言没有原生字符串类型,字符串本质是字符数组,指针是操作它们的高效方式。比如自己实现字符串复制函数:

#include <stdio.h>

void my_strcpy(char *dest, const char *src) {
    // 遍历源字符串,直到遇到结束符'\0'
    while (*src != '\0') {
        *dest = *src;
        dest++;
        src++;
    }
    *dest = '\0'; // 给目标字符串加结束符
}

int main() {
    char source[] = "Hello, Pointer!";
    char destination[50];
    my_strcpy(destination, source);
    printf("复制后的字符串:%s\n", destination);
    return 0;
}

指针可以灵活遍历数组/字符串的每个元素,很多C标准库函数(比如strcpy、strlen)都是基于指针实现的。


4. 构建动态数据结构

像链表、二叉树这类需要动态扩展的数据结构,必须用指针来连接节点:

#include <stdio.h>
#include <stdlib.h>

// 链表节点结构体
typedef struct Node {
    int data;
    struct Node *next; // 指针指向链表的下一个节点
} Node;

// 给链表尾部添加节点
Node* add_node(Node *head, int value) {
    Node *new_node = (Node*)malloc(sizeof(Node));
    new_node->data = value;
    new_node->next = NULL;
    
    if (head == NULL) {
        return new_node; // 空链表,新节点作为头
    }
    
    Node *current = head;
    while (current->next != NULL) {
        current = current->next; // 遍历到最后一个节点
    }
    current->next = new_node;
    return head;
}

// 打印链表
void print_list(Node *head) {
    Node *current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

int main() {
    Node *head = NULL;
    head = add_node(head, 10);
    head = add_node(head, 20);
    head = add_node(head, 30);
    print_list(head);
    
    // 实际使用时要记得遍历链表释放每个节点的内存
    return 0;
}

没有指针的话,根本没法实现这种动态的、可自由扩展的数据结构。


简单总结:你看到的那个示例是错误的示范——指针不是用来做这种冗余赋值的。它的核心作用是让程序能直接操作内存地址,实现那些不用指针就做不到(或者极其繁琐)的功能:修改外部变量、动态内存管理、复杂数据结构构建等。

内容的提问来源于stack exchange,提问作者yuchen liu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 14:05:20