简体   繁体   English

在C中使用前向声明的typedef stuct

[英]typedef stuct with forward declaration in C

I have something like: 我有类似的东西:

typedef struct Data DATA, *DATA_PTR;
typedef struct Units UNITS, *UNITS_PTR;

struct Data
{
    double miscData;
    UNITS units;
};

struct Units
{
    double x[2];
    double y[2];
    double z[2];
};

in my project_typedef.h file. 在我的project_typedef.h文件中。

In another file, I have something like: 在另一个文件中,我有类似的东西:

void fileInput(DATA_PTR data)
{
     //usual declarations and other things
     data->miscData = 0; //Works!
     data->units.x[0] = 5; //Doesn't work
     //etc...
}

However, this doesn't work since units is declared after data in project_typedef.h (if I switch the order it works). 但是,这不起作用,因为单元是在project_typedef.h数据之后声明的(如果我切换它的工作顺序)。 The error that i get is "left of '.x' must have struct/union type". 我得到的错误是“左'.x'必须有struct / union类型”。 I thought that the forward declaration would fix this issue. 我认为前方声明会解决这个问题。 Why not? 为什么不?

When you define Data , all members must be complete types. 定义 Data ,所有成员必须是完整类型。 Since UNITS isn't a complete type at that point, this doesn't work. 由于UNITS在那时不是一个完整的类型,这不起作用。 (By contrast, UNITS_PTR would be fine, since pointers to incomplete types are complete types.) (相比之下, UNITS_PTR 很好,因为指向不完整类型的指针是完整的类型。)

Simply put the Units definition above the Data definition and you should be fine. 只需将Units定义放在Data定义之上,就可以了。

(As @cnicutar already noted, you're also using the array x wrong.) (正如@cnicutar已经注意到的那样,你也使用了数组x错误。)

The forward declaration allows you to use its name in context where an incomplete type is allowed. 前向声明允许您在允许不完整类型的上下文中使用其名称。 Declaring a struct member is not one of such cases, the complete definition must be known as it contributes to the struct layout. 声明结构成员不是这种情况之一,必须知道完整定义,因为它有助于结构布局。

for a struct definition you should always use complete types for all members in a structure... but this is not the case with UNITS units in struct Data ,which declares a variable named units of type struct Units which is never declared before the struct Data... this reflects an error.. you should place the Units definition above Data definition.. and all will work fine.. 对于结构定义,您应该始终对结构中的所有成员使用完整类型...但struct Data UNITS units不是这种情况,它声明了一个名为units类型struct Units的变量,它从未在struct Data之前声明...这反映了一个错误..您应该将Units定义放在Data定义之上..并且所有都可以正常工作..

and regarding forward declaration this does not work since whenever a struct variable is defined, the compiler first allocates the memory required to the struct ( struct members donot have a memory allocated to them, unless they are linked to a struct type of variable .. thats why struct variables cant be initialized inside struct template).. :) 因为无论什么时候定义结构变量,编译器首先分配结构所需的内存( 结构成员不要分配给它们的内存,除非它们链接到结构类型的变量 ..)为什么struct变量不能在struct template中初始化).. :)

There is no prototype for struct. struct没有原型。 This is because compiler needs to know size of struct before using it. 这是因为编译器在使用之前需要知道struct的大小。 You could use pointer on struct, because pointers have known size no matter which type they point to. 你可以在struct上使用指针,因为无论指针指向哪种类型,指针都有已知的大小。

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

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