【问题标题】:Why does "typedef struct anything_at_all boom;" compile without errors? [duplicate]为什么“typedef structanything_at_all 繁荣;”编译没有错误? [复制]
【发布时间】:2018-04-28 20:43:12
【问题描述】:

为什么

//The type anything_at_all exists nowhere.
typedef struct anything_at_all lol;

编译没有错误,而

//The type anything_at_all_2 exists nowhere.
typedef anything_at_all_2 rofl;

产生错误?

结构体有什么特别之处?在某种程度上我很高兴它像这样工作,因为它允许我们创建不透明的结构。但最好能理解为什么它会起作用。

【问题讨论】:

  • 因为struct anything_at_all 是一个不完整的类型。 anything_at_all_2 不是。
  • 编译器无法确认anything_at_all_2 是什么类型的标识符(值或类型,如果是类型,那么是什么类型),而struct anything_at_all 则非常清楚。

标签: c


【解决方案1】:

您可以声明 struct 而不定义它。这称为前向声明。它允许您执行以下操作:

struct s1;

struct s2 {
    int x;
    struct s1 *p;
};

struct s1 {
    int y;
    struct s2 *p;
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2016-01-21
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 2022-11-14
    相关资源
    最近更新 更多