[英]Can a Struct Named “AB” Contain an Array of AB?
typedef struct unit
{
struct unit * next;
int year;
int month;
int day;
struct unit revisions[3];
char subject[100];
}schedule;
The above code is giving me the following error: 上面的代码给我以下错误:
array type has incomplete element type
struct unit revisions[3];
I'm guessing the problem is that a struct cannot contain an array of itself? 我猜问题是结构不能包含自身数组吗? If so, how can I achieve similar functionality?
如果是这样,我如何实现类似的功能?
your question contains the answer itself. 您的问题包含答案本身。
struct unit * next;
You can always use a pointer to the structure type inside the structure definition, and from your function, allocate memory and use it. 您始终可以在结构定义中使用指向结构类型的指针,然后从函数中分配内存并使用它。
This would be a good workaround: 这将是一个很好的解决方法:
typedef struct unit{
struct unit * next;
int year;
int month;
int day;
struct unit *revisions; //just like you do with struct unit *next
char subject[100];
}schedule;
schedule s;
s.revisions = malloc(3 * sizeof *s.revisions);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.