标头中的typedef结构和不完整类型的取消引用指针 [英] typedef struct in header and dereference pointer to incomplete type

查看:113
本文介绍了标头中的typedef结构和不完整类型的取消引用指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C语言很生疏,但我认为我在理解在标头中使用typedef的正确用法,在实现文件中定义实际结构的结构以及在第三个结构中使用该结构时遇到了问题文件.

I'm quite rusty in C, but I think I'm having issues understanding the proper usage of using typedefs in headers, defining the actual struct's structure in the implementation file, and then using the struct in a third file.

我写了一个队列数据结构,其类型定义如下:

I wrote a queue data-structure that has a type defined as such:

typedef struct queue
{
    int count;
    qnode_t *head;
    qnode_t *tail;
} queue_t;

qnode_t是仅在队列的实现文件中使用的结构.

Where qnode_t is a struct that's only used within the queue's implementation file.

在标题中,我有这个:

typedef struct queue queue_t;

当在另一个文件中使用此queue_t类型时,我正在尝试获取队列的长度,如下所示:

When using this queue_t type within another file I'm attempting to get the queue's length like this:

queue_t *work_queue;
...
int length = work_queue->count;

但是,在我要查找的那一行上,我得到了编译器错误:

However, on that line where I'm looking up count I get the compiler error:

取消引用不完整类型的指针

dereferencing pointer to incomplete type

我一直在研究如何正确地在C中定义类型,但是我认为我一直在使自己越来越困惑,而不是变得清晰,因为许多示例要么与其他资源冲突,要么对于它们太简化了.我投入实际使用.

I've been doing a lot of research about how to properly define types in C, but I think I just keep confusing myself more and more instead of getting clarity since many examples are either conflicting with other resources or are too simplified for me to put to practical use.

由于结构中的'count'变量未在其中定义,我会收到此错误吗?如果是这种情况,那么我可以同时在实现和标头中定义结构吗?如果是这样,则自头&头以来,标头只能定义计数变量.尾巴应该是隐藏的/私人的吗? (我很想念OOP)我应该只做另一个使用queue_t*并将其长度返回为size_t的函数吗?

Would I be getting this error because the 'count' variable within the struct isn't defined there? If this is the case, then can I define the struct in BOTH the implementation and header? If so, can the header only have the count variable defined since the head & tail should be hidden/private? (I miss OOP) Should I just be making another function that takes a queue_t* and returns its length as size_t?

推荐答案

,您只能取消引用已定义的类型,而不能取消声明的类型. 类型声明对于检查不透明指针的类型很有用,但是 对象字段不可见,无法访问.您需要将typedef移到标题中 访问队列对象的字段.

you can only dereference defined types, not declared types. Type declarations are useful to type-check opaque pointers, but object fields are not visible, cannot be accessed. You need to move the typedef into the header to access fields of your queue object.

来自以下问题/答案:

是的,两个相同的结构定义被视为相同的typedef.如果您从未在同一个源文件中同时拥有两个定义,则可以省略字段,但不要这样做,这会导致错误和维护混乱.最好使用命名约定,例如以下划线开头的名称是内部名称.

Yes, two identical struct definitions are seen as the same typedef. You could omit fields if you never had both definitions in the same source file, but don't do it, that leads to bugs and maintenance confusion. Better to use a naming convention eg names starting with underscores are internal.

惯例是在标头中定义结构,然后在实现文件中包含相同的标头.这样可以使发布的版式与实现保持同步

The convention is to define the struct in the header then include the same header in the implementation file. This keeps the published layout in sync with the implementation

这篇关于标头中的typedef结构和不完整类型的取消引用指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆