【发布时间】:2012-03-31 07:00:33
【问题描述】:
这段代码:
extern void *malloc(unsigned int);
struct Box {
int x, y ,w, h;
};
struct Wall {
char color[15];
struct Box *boxes[20];
};
int main(int argc, const char *argv[])
{
struct Wall *mywall = malloc(sizeof(struct Wall));
struct Box *myboxes[] = mywall->boxes;
return 0;
}
在第 14 行给我invalid initializer 错误。我想要做的是获取结构指针数组的副本,这些指针位于不同的结构中。
【问题讨论】:
-
您不能在 C 中复制或分配数组。(但是,您可以将它们包装到一个结构中,可以被分配。)
-
为什么不用
#include <stdlib.h>而不是extern void *malloc(unsigned int);? -
@glglgl 没有特别的原因。我写这段代码只是为了证明我的观点。我忘了malloc在哪里,懒得找了。
标签: c arrays pointers struct initialization