C语言 如何在头文件中引用 typedef?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20120833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:22:34  来源:igfitidea点击:

How do I refer to a typedef in a header file?

cheadertypedef

提问by theva

I have a source file where a typedef struct is defined:

我有一个定义了 typedef 结构的源文件:

typedef struct node {
    char *key;
    char *value;
    struct node *next;
} *Node;

In this module, there are some functions that operate on a Node and have Node as return type. What am I supposed to write in the header file for this typedef?

在这个模块中,有一些函数在 Node 上运行,返回类型为 Node。我应该在这个 typedef 的头文件中写什么?

Is it correct to write just

只写正确吗

typedef *Node;

in the header?

在标题中?

回答by user694733

You can use:

您可以使用:

typedef struct node * Node;

But I would advise against hiding the pointer in type declaration. It is more informative to have that information in variable declaration.

但我建议不要在类型声明中隐藏指针。在变量声明中包含该信息会提供更多信息。

module.c:

模块.c:

#include "module.h"
struct node {
    char *key;
    char *value;
    struct node *next;
};

module.h:

模块.h:

typedef struct node Node;

variable declaration for pointer somewhere:

某处指针的变量声明:

#include "module.h"
Node * myNode; // We don't need to know the whole type when declaring pointer

回答by Filipe Gon?alves

The declaration

声明

typedef *Node;

is invalid. It's missing the type. A good way to look at typedefis to remember it is just a storage class specifier, thus, it works like any other declarations. If you want to create an alias Nodefor the type struct node *, just declare a node pointer and name it Node:

是无效的。它缺少类型。一个很好的查看方式typedef是记住它只是一个存储类说明符,因此,它的工作方式与任何其他声明一样。如果要Node为该类型创建别名struct node *,只需声明一个节点指针并将其命名为Node

struct node *Node;

struct node *Node;

And now prepend a typedefkeyword:

现在添加一个typedef关键字:

typedef struct node *Node;

typedef struct node *Node;

In general, to create an alias for a type, you just declare a variable of that type with the same name as the alias name you wish, and then prepend a typedefkeyword. Even complicated typedefdeclarations can be easily approached like this.

通常,要为类型创建别名,只需声明该类型的变量,其名称与您希望的别名名称相同,然后在前面加上typedef关键字。即使是复杂的typedef声明也可以像这样轻松处理。

So, typedef *Node;is invalid, it's like writing just j;without actually specifying its type.

所以,typedef *Node;是无效的,就像只是在j;没有实际指定其类型的情况下进行写作。

Now, you can't typedefsomething twice. Either you will have to take the typedefout of struct nodedeclaration and move it to the header file, or you move the whole typedef+ structure declaration to the header file. The former solution is generally what you want, since it allows you to have some information hiding. So, my suggestion is to write this in the header file:

现在,你不能typedef做两次。您要么必须取出typedefout ofstruct node声明并将其移动到头文件中,要么将整个typedef+ 结构声明移动到头文件中。前一种解决方案通常是您想要的,因为它允许您隐藏一些信息。所以,我的建议是在头文件中写这个:

typedef struct node *Node_ptr;

And keep this in the source file:

并将其保存在源文件中:

struct node {
    char *key;
    char *value;
    struct node *next;
};

Note that I created the alias Node_ptr, not Node. This is good practice, to convey the idea that Node_ptris a pointer.

请注意,我创建了别名Node_ptr,而不是Node. 这是一个很好的做法,传达的想法Node_ptr是一个指针。