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

C语言结构体数组传入函数后赋值不生效的原因及解决方法

问题分析与解决

为什么t.arr[0]没被赋值为1?

有两个核心问题:

  • 值传递导致修改无效:foo函数的参数是struct test t,这是值传递,函数内部会创建一个传入结构体的副本。你修改的只是这个副本的arr[0],main函数里的原结构体完全不受影响。
  • 数组初始化方式错误:struct test t = {malloc(10*sizeof(int))};是错误写法。结构体里的arr是固定大小的数组,不是指针,它的内存和结构体一起分配,不需要用malloc申请。这里用指针值(malloc返回的地址)初始化int数组,会把地址数值拆分成int元素赋值给arr,属于非法初始化,会导致arr初始值混乱。

实现需求的正确写法

方法1:传结构体指针给函数(推荐)

把foo的参数改成指针,直接操作原结构体:

#include <stdio.h>

struct test{
    int arr[10];
};

void foo(struct test *t){
    t->arr[0] = 1; // 通过指针访问结构体成员
}

int main() {
    struct test t = {0}; // 正确初始化数组为全0
    foo(&t); // 传入结构体的地址
    printf("%d", t.arr[0]); // 输出1
    return 0;
}

方法2:让函数返回修改后的结构体

如果不想用指针,可以让foo返回修改后的结构体,再赋值给原变量:

#include <stdio.h>

struct test{
    int arr[10];
};

struct test foo(struct test t){
    t.arr[0] = 1;
    return t;
}

int main() {
    struct test t = {0};
    t = foo(t); // 接收返回的修改后的结构体
    printf("%d", t.arr[0]); // 输出1
    return 0;
}

注意:第二种方法需要复制整个结构体,当结构体体积较大时效率较低,优先推荐第一种传指针的方式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 13:02:07