简体   繁体   English

名为“ AB”的结构体可以包含AB数组吗?

[英]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.

相关问题 二进制*的操作数无效(有'ab {aka struct a}'和'ab * {aka struct a *}') - Invalid operands to binary * (have ‘ab {aka struct a}’ and ‘ab * {aka struct a *}’) 动态2D数组的C结构:对象0x7ffeee94ab80的错误:释放的指针未分配 - C struct for dynamic 2d array: error for object 0x7ffeee94ab80: pointer being freed was not allocated 从 C 中的二进制文件(使用 ab+)读取后如何打印结构成员 - How does one print a struct member after reading from binary file (using ab+) in C C 结构体是否可以包含指向其他结构体数组的指针(长度可变) - Can a C struct contain a pointer to an array of other structs (with a varying length) 分配`ab`时包含单个字符的char - char containing single character when assigning `ab` fopen()中的“ rb +”和“ ab”有什么区别? - What is the difference between “rb+” and “ab” in fopen()? char * str =“ ab”,str和&str的混淆 - confusion of char* str=“ab”, str and &str 在汇编中为正则表达式'[ab] [^ r] + r]'实现匹配器 - Implementing a matcher for the regex '[ab][^r]+r]' in assembly fopen(argv[1], "ab") 后 fseek 函数不工作 - fseek function is not working after fopen(argv[1], "ab") 这在C中意味着什么? AB-> CD-> Func键(PARAM) - What does this means in C? AB->CD->Func(param)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM