【问题标题】:Why are pointers to incomplete types allowed and not variables of incomplete types?为什么允许指向不完整类型的指针而不是不完整类型的变量?
【发布时间】:2017-01-19 20:52:24
【问题描述】:

为什么以下是合法的:

typedef struct a aType;
struct a
{
   int x;
   aType *b;
};

以及以下违法行为:

void main()
{
    typedef struct a aType;
    aType someVariable;
    struct a
    {
      int x;
      aType *b;
    };
}

我只是好奇,因为在每种情况下它都是前向引用,据我所知,至少对于函数和变量,前向引用是不合法的。

另外,C++ 的答案是否也一样?

【问题讨论】:

  • 在情况 2 中,编译器还不知道 aType 的大小 — 在您尝试定义 someVariable 后出现。但是,您可以合法地使用 aType *somePointer; — 指针大小是已知的,即使它指向的大小不知道。
  • 请使用更对应的标题。这与你所问的完全无关。如果您对不同的语言感兴趣,请提出单独的问题。
  • @JonathanLeffler 函数 main 在托管环境中可以有任何类型。
  • @EugeneSh.: 是的,但是指向所有 struct 类型的指针将具有相同的大小和表示形式 (6.2.5/28)。

标签: c typedef forward-declaration


【解决方案1】:

您可以创建指向不完整类型的指针,因为指针对象的大小不依赖于指向类型的大小。指向不同struct 类型的指针具有相同的大小和表示形式,而与struct 类型本身的大小无关。

您不能创建不完整类型的实例,因为类型的大小是未知的。

【讨论】:

【解决方案2】:

这个是这样的:

typedef struct a aType;
struct a { int x; aType *b; };

等同于:

struct a;
typedef struct a aType;
struct a { int x; aType *b; };

因此,您正在前向声明 structtypedef 并随后对其进行定义。完全没问题。

现在是第二个例子:

typedef struct a aType;
aType someVariable;
struct a { int x; aType *b; };

这与它在本地范围内的事实无关。 发生了什么是这样的:

struct a;
typedef struct a aType;
aType someVariable; // error: when it gets here, aType is still incomplete
struct a { int x; aType *b; };
aType someVariable; // perfectly fine, aType not incomplete

请记住,编译是按顺序进行的。当您尝试声明 someVariable 时,编译器还不知道 struct a 是什么,所以它不知道它的大小,因此它不知道要为其分配多少内存,因此会出现编译错误。在定义aType 之后声明它可以按预期工作。

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 2012-08-15
    • 2015-10-09
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多