【问题标题】:C90 and typedefC90 和 typedef
【发布时间】:2010-12-21 13:53:42
【问题描述】:

我定义了struct point {(...)};。但是对于 C90,我似乎必须使用 typedef。我该如何正确地做到这一点? typedef struct point {} point;? typedef struct {} point;? typedef struct point {};?

【问题讨论】:

    标签: c struct typedef c89


    【解决方案1】:

    你可以这样做:

    typedef struct Point { ... } MyPoint;
    

    然后使用这两种声明:

    struct Point p1;
    MyPoint p2;
    

    【讨论】:

    • 感谢您的澄清!
    • struct 名称和 typedef 名称位于不同的命名空间中,因此您可以使用 typedef struct T {...} T;。这也是为什么您在声明 struct point {...}; 后尝试使用 point p; 失败的原因:您必须使用 struct 关键字声明 struct point 类型的变量:struct point p = {0, 0};
    【解决方案2】:

    这两个都是正确的:

    typedef struct point { /* ... */ } point;
    typedef struct { /* ... */ } point;
    

    第一个版本定义struct point,然后定义point作为它的别名,而第二个版本定义point作为匿名结构的别名。

    【讨论】:

      猜你喜欢
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多