首页 > 解决方案 > 如何以非标准方式在 C 中键入定义结构

问题描述

我在以非标准方式在 C 中定义结构时遇到问题。

我正在编写自己的内核。

struct Time {
    int sec;
    int min;
    int hour;
    int monthDay;
    int month;
    // Year count from 1900
    int year;
    // Week day <0; 6>; 0 - sunday...6 - saturday
    int weekDay;
    // Year day is counted from 0
    int yearDay;
    // If > 0 then there is summer time, if 0 then there is standard time,
    // if < 0 then there isn't info about time
    int timeZone;
};

typedef struct Time struct tm;     // Doesn't work :(((

我这里有一个编译错误。我想以两种方式定义 struct Time 变量:

我希望之前有能力编写结构,tm b;因为我想标记类型 tm 是结构而不是简单的原始类型。

标签: cstructtypedef

解决方案


你试图做的事情在很多方面都很奇怪。我看不到一个好的用例,但既然你问我会提供一个解决方案。但我强烈反对这样做。

#define tm Time

struct Time {
    int sec;
};

struct Time a;
struct tm b;

用常规 typedef 解决这个问题是不可能的。


推荐阅读