小编Cly*_*yde的帖子

结构体中的数组传递给函数

我对 C 还很陌生,刚刚开始学习。如果这个问题看起来有点愚蠢,我深表歉意。

有人可以解释一下为什么这不起作用吗?有什么办法可以完成这样的事情吗?非常感谢!

struct test{
    int arr[10];
};

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

int main() {
    struct test t = {malloc(10*sizeof(int))};
    foo(t);
    printf("%d", t.arr[0]);
}
Run Code Online (Sandbox Code Playgroud)

我不确定为什么 t.arr[0] 没有分配给 1。

c struct arr

2
推荐指数
1
解决办法
106
查看次数

标签 统计

arr ×1

c ×1

struct ×1