gpt4 book ai didi

c - HiC : Structure within Structure?

转载 作者:行者123 更新时间:2023-12-02 08:44:50 25 4
gpt4 key购买 nike

这是我第一次在结构中使用结构。我在编译我的程序时遇到了这个错误。错误:字段“结果”的类型不完整。

错误是指这行代码。-->结构result_t结果;

有什么帮助吗? :)谢谢。

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

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

编辑:

我更改了我的代码:

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

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

我得到了一个新的编译错误。错误:下标值既不是数组也不是指针。

触发该错误的代码行如下。printf("%-7s %-2s %d\n", student.results[i].module_code, student.results[i].grade, student.results[i].mc);

最佳答案

结果不是数组。你应该改变结构学生:

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

或者将 printf 更改为:

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

这取决于一个学生可能有多少种结果。

关于c - HiC : Structure within Structure?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13247853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com