简体   繁体   English

C语言中结构和功能内的结构

[英]Structure within structure and function in C

I've been doing the task of reading data and creating particular structures. 我一直在做读取数据和创建特定结构的任务。 In one structure (which contains in itself another structure) eclipse shows "field 'birth' has incomplete type". 在一个结构(本身包含另一个结构)中,日食显示“字段'出生'的类型不完整”。 I've searched through web, but it looks like there is some specific mistake. 我已经在网上搜索过,但似乎有一些特定的错误。 (Here is shortened version of the code) (以下是代码的简化版本)

typedef struct{
    int birthday_day;
    int birthday_month;
    int birthday_year;
} birthday;

typedef struct{
    int id;
    char name[20];
    struct birthday birth;

}user;

user usser[100];
int i;

for (i=0;i<100;i++){
    fscanf(input, "%s %i %i %i %i", usser[i].id,  
           usser[i].name, usser[i].birth.birthday_day, usser[i].birth.birhday_month,
           usser[i].birth.birthday_year
};
typedef struct _birthday{
    int birthday_day;
    int birthday_month;
    int birthday_year;
} birthday;

typedef struct{
    int id;
    char name[20];
    struct _birthday birth;

}user;

or 要么

typedef struct{
    int id;
    char name[20];
    birthday birth;

}user;

in your example "birthday" is a new type which doesn't need the keyword "struct". 在您的示例中,“生日”是一种不需要关键字“ struct”的新类型。 That's why you get the error. 这就是为什么您得到错误。 You can use this type or give a name to the struct and use it with the keyword struct. 您可以使用此类型或为结构命名,然后将其与关键字struct一起使用。

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

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