简体   繁体   English

C向前声明typedef结构

[英]C forward declaration for typedef struct

I'm trying to forward declare typedef struct wheels. 我正在尝试向前声明typedef结构轮。

typedef struct wheels wheels;

typedef struct car {
  float topSpeed;
  wheels w;
} car;

typedef struct wheels {
  int frontWheels;
  int backWheels;
} wheels; 

int main() {
  car c = {
    .topSpeed = 255.0,
    .w = {
      .frontWheels = 2,
      .backWheels = 2,
    }
  };

  return 0; 
}

This gives me the following errors: 这给了我以下错误:

error: field 'w' has incomplete type wheels w; 错误:字段“ w”的车轮w的类型不完整;

error: field name not in record or union initializer .frontWheels = 2 错误:字段名称不在记录或联合初始化程序中。frontWheels= 2

error: field name not in record or union initializer .backWheels = 2 错误:字段名称不在记录或联合初始化程序中。backWheels= 2

I know I can move the entire typedef struct wheels above the typedef struct car and it would work. 我知道我可以将整个typedef struct车轮移到typedef struct车上方,并且它可以工作。

How do I forward declare struct wheels correctly? 如何正确声明结构轮?

Here are the relevant sections from the C standard (emphasis added): 以下是C标准的相关部分(添加了重点):

§6.2.5p1 §6.2.5p1

At various points within a translation unit an object type may be incomplete (lacking sufficient information to determine the size of objects of that type) or complete (having sufficient information). 在翻译单元内的各个点,对象类型可能不完整(缺少足够的信息来确定该类型对象的大小)或完整(具有足够的信息)。

§6.7.2p3 §6.7.2p3

A structure or union shall not contain a member with incomplete or function type (hence, a structure shall not contain an instance of itself, but may contain a pointer to an instance of itself), except that the last member of a structure with more than one named member may have incomplete array type; 结构或联合不得包含具有不完整或函数类型 的成员 (因此,结构不应包含其自身的实例,但可以包含指向其自身的实例的指针),但结构的最后一个成员具有大于一个命名成员可能具有不完整的数组类型; such a structure (and any union containing, possibly recursively, a member that is such a structure) shall not be a member of a structure or an element of an array. 这样的结构(以及任何可能包含递归的此类结构的并集的联合)不应是结构的成员或数组的元素。

The first typedef declares an incomplete type called wheels . 第一个typedef声明一个不完整的类型,称为wheels The car structure uses that incomplete type as a member. car结构使用该不完整类型作为成员。 That's explicitly forbidden by the standard. 这是标准明确禁止的。

That's what the first error message is telling you. 这就是第一条错误消息告诉您的内容。 The other two error messages are just noise. 其他两个错误消息只是噪音。 They are the result of the fact that the compiler did not have sufficient information to complete the car structure. 它们是由于编译器没有足够的信息来完成car结构的结果。

As mentioned in the other answer, one of the uses of incomplete types is to declare pointers. 如另一个答案中所述,不完整类型的用途之一是声明指针。 For example, a node in a linked list where the structure contains a pointer to itself: 例如,链表中的一个节点,其中结构包含指向其自身的指针:

typedef struct node Node;   // struct node and Node are incomplete types here

struct node
{
    int value;
    Node *next;             // using an incomplete type to declare a pointer
};                          // struct node and Node are complete from here forward

您只能拥有指向不完整的前向定义结构或联合的指针

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM