嵌套结构的结构类型不完全

时间:2019-03-21 01:56:15

标签: c arrays pointers malloc structure

我在结构上没什么问题。这是我的结构块:

#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;

我尝试使用嵌套的结构和指针。 例如,要获得课程的配额,我可以在主要功能中使用简单块,如下所示:

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);

我不知道如何解决“将指针转换为不完整类型”,因为它与指针有些混淆。我应该使用内存分配吗?

1 个答案:

答案 0 :(得分:0)

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];