【发布时间】:2019-04-23 17:39:49
【问题描述】:
这是我到目前为止所做的事情
struct test_case {
int n;
int *test[];
};
struct test_case *test_case_struct = (struct test_case *)malloc(
sizeof(struct test_struct) + 100 * sizeof(int));
我需要在“测试”指针数组中分配 n 个指针。据我所知,我需要为结构分配空间,然后为指针数组分配更多空间,但是当我尝试编译它时,我得到了错误 对不完整类型的 struct test_struct 无效使用 sizeof 运算符
如果有人可以告诉我如何将 n 的值作为用户输入 并使 int *test [n] 成为可能。
【问题讨论】:
-
在创建结构时尝试使用 typedef,然后在 sizeof 中使用类型名称。
-
旁注,但请考虑not casting the result of malloc。
标签: c data-structures dynamic-memory-allocation