简体   繁体   English

HiC:结构中的结构?

[英]HiC : Structure within Structure?

this is my first time using structure within a structure. 这是我第一次在结构中使用结构。 I encounter this error when I compile my program. 编译程序时遇到此错误。 error: field 'results' has incomplete type. 错误:字段“结果”的类型不完整。

The error is referring to this line of code. 错误是指这行代码。 -->struct result_t results; ->结构result_t结果;

Any help please? 有什么帮助吗? :) Thanks. :) 谢谢。

typedef struct {
char moduleCode[8];
char grade[3];
} result_t;

typedef struct {
char name[31];
struct result_t results;
} student_t;

Edit: 编辑:

I changed my codes: 我更改了代码:

typedef struct {
char moduleCode[8];
char grade[3];
} result_t;

typedef struct {
char name[31];
result_t results;
} student_t;

and I got a new compilation error. 然后出现新的编译错误。 error : subscripted value is neither array nor pointer. 错误:下标值既不是数组也不是指针。

The line of code that triggered that error is as follows. 触发该错误的代码行如下。 printf(" %-7s %-2s %d\\n", student.results[i].module_code, student.results[i].grade, student.results[i].mc); printf(“%-7s%-2s%d \\ n”,student.results [i] .module_code,student.results [i] .grade,student.results [i] .mc);

Result is not an array. 结果不是数组。 you should either change the structure student with: 您应该使用以下方式更改学生的结构:

typedef struct {
    char name[31];
    result_t results[MAX_NUM_RESULTS];
} student_t;

Or change the printf to: 或将printf更改为:

printf(" %-7s %-2s %d\n", student.results.module_code, student.results.grade, student.results.mc);

It depends on how many possible results one student may have. 这取决于一个学生可能获得多少结果。

since you are using typedef use this 由于您使用的是typedef,因此请使用

typedef struct {
char name[31];
result_t results;<---remove struct
} student_t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM