【发布时间】:2015-11-03 20:08:04
【问题描述】:
你能解释一下如何在 typedef struct 中使用 int 数组吗?
在我的标题中我有代码:
typedef struct {
int arr[20];
int id;
} Test;
在某些函数中(我包含我的头文件)我使用:
Test tmp = malloc(sizeof(Test));
tmp.id = 1;
//and how to use array arr?
//for example I want add to array -1
感谢您的回复。
【问题讨论】:
-
tmp.arr[0] = -1?您必须跟踪最后一个索引,因为您无法将内容“添加”到数组中。它们的大小是固定的(除非你使用realloc) -
执行此命令后出现分段错误(核心转储)错误。
-
显示的代码不会编译。