嵌套结构的不完整结构类型问题
Incomlete type of structure problem with nested structures
我在结构方面几乎没有问题。这是我的结构块:
#define STD_NAME 30
#define COURSE_LIMIT 10
#define COURSE_NAME 50
#define COURSE_CODE 6
#define COURSE_ACRONYM 8
typedef struct {
int course_id;
char* course_name[COURSE_NAME];
char* course_code[COURSE_CODE];
char* course_acronym[COURSE_ACRONYM];
int quota;
}course_t;
typedef struct {
int std_id;
char std_name[STD_NAME];
double std_gpa;
struct course_t* courses[COURSE_LIMIT]; //nesting part
}student_t;
我尝试使用嵌套结构和指针。
例如,为了获得课程的配额,我在 main 函数中使用了这样的简单块:
int main(void){
student_t studentProfile;
for(int i = 0; i < COURSE_LIMIT; i++)
{
printf("Enter the %d. course quota: ", i + 1);
scanf("%d", &studentProfile.courses[i]->quota);
}
return 0;
}
但是当我编译这段代码时,出现如下错误:
dereferencing pointer to incomplete type ‘struct course_t’
scanf("%d", &studentProfile.courses[i]->quota);
我不知道如何修复 'deferencing pointer to incomplete type' 因为它与指针有点混淆。我应该使用内存分配吗?
typedef struct {
int std_id;
char std_name[STD_NAME];
double std_gpa;
struct course_t* courses[COURSE_LIMIT]; //nesting part
//^^^^^^^^^^^^^^^^
}student_t;
此时没有struct course_t
这样的类型。只有类型course_t
;它们不可互换。
那一行应该是
course_t* courses[COURSE_LIMIT];
我在结构方面几乎没有问题。这是我的结构块:
#define STD_NAME 30
#define COURSE_LIMIT 10
#define COURSE_NAME 50
#define COURSE_CODE 6
#define COURSE_ACRONYM 8
typedef struct {
int course_id;
char* course_name[COURSE_NAME];
char* course_code[COURSE_CODE];
char* course_acronym[COURSE_ACRONYM];
int quota;
}course_t;
typedef struct {
int std_id;
char std_name[STD_NAME];
double std_gpa;
struct course_t* courses[COURSE_LIMIT]; //nesting part
}student_t;
我尝试使用嵌套结构和指针。 例如,为了获得课程的配额,我在 main 函数中使用了这样的简单块:
int main(void){
student_t studentProfile;
for(int i = 0; i < COURSE_LIMIT; i++)
{
printf("Enter the %d. course quota: ", i + 1);
scanf("%d", &studentProfile.courses[i]->quota);
}
return 0;
}
但是当我编译这段代码时,出现如下错误:
dereferencing pointer to incomplete type ‘struct course_t’
scanf("%d", &studentProfile.courses[i]->quota);
我不知道如何修复 'deferencing pointer to incomplete type' 因为它与指针有点混淆。我应该使用内存分配吗?
typedef struct {
int std_id;
char std_name[STD_NAME];
double std_gpa;
struct course_t* courses[COURSE_LIMIT]; //nesting part
//^^^^^^^^^^^^^^^^
}student_t;
此时没有struct course_t
这样的类型。只有类型course_t
;它们不可互换。
那一行应该是
course_t* courses[COURSE_LIMIT];