另一个typedef结构

时间:2016-03-02 15:15:21

标签: c struct typedef

如何在其他typedef struct中声明typedef struct

typedef struct {
    char* type;
    char* destination;
    float price;
    typedef struct {
        int date;
        int month;
        int year;
    }date;
}Offer;

我试过这样的事情,但我不知道它是否正确。我想在typedef struct中声明另一个日期格式的typedef结构。

3 个答案:

答案 0 :(得分:2)

试试这个:

typedef struct {
  char* type;
  char* destination;
  float price;
  struct {
    int day;
    int month;
    int year;
  } date;
} Offer;

这将date定义为匿名结构。如果您需要在其他地方使用此结构,请使用Shark's approach

答案 1 :(得分:1)

不要写你写的东西,试试这样的事情:

typedef struct {
    int date;
    int month;
    int year;
} OfferDate;


typedef struct {
char* type;
char* destination;
float price;
OfferDate date; 
} Offer;

答案 2 :(得分:0)

这是不正确的语法。如果要使用另一个结构定义结构,可以这样做:

typedef struct {
    int a1;
    int a2;
} ExampleStruct1;

typedef struct {
    int b1;
    ExampleStruct1 b2;
} ExampleStruct2;
相关问题