C语言 typedef 结构清晰
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5552394/
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
typedef struct clarity
提问by Kyel John M David Philippines
I am getting confused with typedef can anyone transcribe this to a normal composition? of structures? I really don't want to handle typedef since it's gets me confuse
我对 typedef 感到困惑,有人可以将其转录为正常的组合吗?结构?我真的不想处理 typedef 因为它让我感到困惑
struct stackNode
{
int data;
struct stackNode *nxtptr;
};
typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;
is
是
typedef struct stackNode StackNode;is the same as struct stackNode StackNodeand typedef StackNode *StackNodePtr;is the same as struck stackNode *StackNodePtr??
typedef struct stackNode StackNode;是一样的struct stackNode StackNode,typedef StackNode *StackNodePtr;是一样的 struck stackNode *StackNodePtr??
回答by Pablo Santa Cruz
If you don't want to use typedefyou can always use full type name:
如果您不想使用,typedef您可以随时使用完整的类型名称:
Instead of:
代替:
StackNode sn;
You would use:
你会使用:
struct stackNode sn;
Instead of:
代替:
StackNodePtr snp;
You would use:
你会使用:
struct stackNode *snp;
The declarations are exactly the same.
声明完全相同。
回答by Lundin
A more common way to write the very same would be:
一种更常见的写法是:
typedef struct stackNode
{
int data;
struct stackNode *nxtptr;
} StackNode_t;
where stackNodeis the so called "struct tag" and StackNode_tis the actual name of the type. If you declare structs like this, the rest of the program won't need to concern itself with the struct tag, and you can use nxtptr as if it was of StackNode_t.
哪里stackNode是所谓的“结构标记”,StackNode_t是类型的实际名称。如果你像这样声明结构体,程序的其余部分就不需要关心结构体标签,你可以像使用 StackNode_t 一样使用 nxtptr。
回答by Indinfer
typedef struct stackNode { int data; struct stackNode *nxtptr; } StackNode_t;
typedef struct stackNode { int data; struct stackNode *nxtptr; } StackNode_t;
If you do not want to have "struct stackNode" inside the struct, I found that I am able to do this:
如果您不想在结构中包含“struct stackNode”,我发现我可以这样做:
typedef struct STACKNODE STACKNODE;
typedef struct STACKNODE
{
int data;
STACKNODE *nxtptr;
};
That you could code such a thing, AND it compiles just fine, AND it runs just fine may seem counter intuitive. It can be counter intuitive because I'm using the same name for struct STACKNODE and the typedef STACKNODE. And further, I am typedef-ingthe struct beforeit exists as a definition. Nevertheless, I have found that I can do this with Microsoft's C through many versions to today and Borland C (way back then).
您可以编写这样的代码,并且它编译得很好,并且运行得很好,这似乎违反直觉。它可能与直觉相反,因为我对 struct STACKNODE 和 typedef STACKNODE 使用相同的名称。进一步,我typedef-荷兰国际集团的结构之前,它存在一个定义。尽管如此,我发现我可以使用 Microsoft 的 C 通过许多版本做到这一点,直到今天和 Borland C(当时)。
I like it because if I am already going to typedef the struct, I don't like resorting to "struct STACKNODE *nxtptr" (i.e. using the word "struct") inside the struct definition.
我喜欢它,因为如果我已经打算对结构进行 typedef,我不喜欢在结构定义中诉诸“struct STACKNODE *nxtptr”(即使用“struct”一词)。
回答by tamasgal
With typedef, you define StackNodeto be struct stackNode, and the Pointer StackNodePtrto be StackNode.
使用 typedef,您将StackNode定义为struct stackNode,将指针StackNodePtr 定义为StackNode。
So what is not clear?
那么有什么不清楚的呢?
回答by Chris
Well, to explain it easily: Typedef does basically nothing else then to tell the compiler that you create a new type of variables (such as int, char etc..)
好吧,简单解释一下:Typedef 基本上什么都不做,只是告诉编译器您创建了一种新类型的变量(例如 int、char 等)。
The main reasons why to use typedef are
使用 typedef 的主要原因是
- Mnemonic names. This is often used in the library functions as well. Instead of using a standard type like int or long you can just typedef it into a size_t. So it's clearer what this variable is used for.
- Shortening names. Commonly used for things like structs (this would be your case). To avoid always having to type struct myStruct varname you can easily use a typedef to get rid of the struct in front.
- 助记名。这也经常用于库函数中。而不是使用像 int 或 long 这样的标准类型,你可以将它 typedef 到 size_t 中。所以这个变量的用途就更清楚了。
- 缩短名称。通常用于结构之类的东西(这就是你的情况)。为了避免总是输入 struct myStruct varname,您可以轻松地使用 typedef 来摆脱前面的结构。

