C 中带有前向声明的 typedef Stuct

发布于 2024-12-16 00:32:36 字数 643 浏览 4 评论 0原文

有类似的内容

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];
};

我的 project_typedef.h 文件中

。在另一个文件中,我有类似的内容:

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

但是,这不起作用,因为单位是在 project_typedef.h 中的数据之后声明的(如果我切换它的工作顺序)。我得到的错误是“'.x'的左侧必须具有结构/联合类型”。我认为前瞻性声明可以解决这个问题。为什么不呢?

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.

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). The error that i get is "left of '.x' must have struct/union type". I thought that the forward declaration would fix this issue. Why not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

捶死心动 2024-12-23 00:32:36

当您定义Data时,所有成员都必须是完整类型。由于 UNITS 此时还不是完整的类型,因此这是行不通的。 (相比之下,UNITS_PTR就可以了,因为指向不完整类型的指针是完整类型。)

只需将Units定义放在之上数据 定义,你应该没问题。

(正如 @cnicutar 已经指出的那样,您也错误地使用了数组 x 。)

When you define Data, all members must be complete types. Since UNITS isn't a complete type at that point, this doesn't work. (By contrast, UNITS_PTR would be fine, since pointers to incomplete types are complete types.)

Simply put the Units definition above the Data definition and you should be fine.

(As @cnicutar already noted, you're also using the array x wrong.)

空城旧梦 2024-12-23 00:32:36

前向声明允许您在允许不完整类型的上下文中使用其名称。声明结构成员不是此类情况之一,必须了解完整的定义,因为它有助于结构布局。

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.

零時差 2024-12-23 00:32:36

对于结构体定义,您应该始终对结构体中的所有成员使用完整类型...但是 struct Data 中的 UNITS 单位 的情况并非如此,它声明了一个变量名为 unitsstruct Units 类型从未在 struct Data 之前声明...这反映了一个错误。您应该放置 Units 定义上面的 Data 定义..一切都会正常工作..

关于前向声明,这不起作用,因为每当定义结构变量时,编译器首先分配结构所需的内存(结构成员没有分配给它们的内存,除非它们链接到结构类型的变量..这就是为什么结构变量不能在结构模板内初始化)..:)

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..

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).. :)

这个俗人 2024-12-23 00:32:36

结构没有原型。这是因为编译器在使用它之前需要知道结构的大小。您可以在结构上使用指针,因为无论指针指向哪种类型,它们都具有已知的大小。

There is no prototype for struct. This is because compiler needs to know size of struct before using it. You could use pointer on struct, because pointers have known size no matter which type they point to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文