0

我有一个结构,它有一个结构作为成员,我想从第一个结构访问该成员。你没明白吗?我给你看。

typedef struct 
{
    int day;
} Date;

typedef struct 
{
    struct Date;
} Insert;

Insert insert;

scanf("%d", &insert.day); // I tried this but it doesn't work
scanf("%d", &insert.date.day); // Figured maybe this would do it, but nope
4

2 回答 2

0

正如其他人所说,您编写代码的方式不会构建。您应该收到如下所示的错误:

typedef struct 
{
    int day;
} Date;

typedef struct 
{
    struct Date;//Error here - Undefined size for field, incomplete struct Date defined at cfile.c:8
} Insert;

Insert insert;

int main(void)
{
    return 0;   
}

您已经创建了一个类型Date(即typedef struct...Date),请使用它而不是struct Date;这样:( 这将构建)

#include <ansi_c.h>
typedef struct 
{
    int day;
} Date;//you have just created a new type:  struct Date here..., use "Date date;" below, (not struct Date;)

typedef struct 
{
    Date date;//"Date is a type (typedef struct Date), so use it here to declare the member "date"
} Insert;

Insert insert;

int main(void)
{
    return 0;   
}  

int main(void)
{
      scanf("%d", &insert.date.day);
      return 0; 
}
于 2013-11-12T21:05:53.640 回答
0

你需要 :

typedef struct 
{
    Date date;
} Insert;

Insert insert;

然后,

scanf("%d", &insert.date.day);
于 2013-11-12T20:49:42.903 回答