typedef 结构清晰
我对 typedef 感到困惑,有人可以将其转录为正常的组合吗?的结构?我真的不想处理 typedef,因为它让我感到困惑的
struct stackNode
{
int data;
struct stackNode *nxtptr;
};
typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;
是
typedef struct stackNode StackNode;
与 struct stackNode StackNode
相同 并且 typedef StackNode *StackNodePtr;
与 struck stackNode *StackNodePtr
相同?
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
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 StackNode
and typedef StackNode *StackNodePtr;
is the same as struck stackNode *StackNodePtr
??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您不想使用
typedef
您始终可以使用完整类型名称:而不是:
您将使用:
而不是:
您将使用:
声明完全相同。
If you don't want to use
typedef
you can always use full type name:Instead of:
You would use:
Instead of:
You would use:
The declarations are exactly the same.
编写相同内容的更常见方法是:
其中
stackNode
是所谓的“结构标记”,StackNode_t
是类型的实际名称。如果您像这样声明结构,则程序的其余部分将不需要关心结构标记,并且您可以使用 nxtptr,就像它是 StackNode_t 一样。A more common way to write the very same would be:
where
stackNode
is the so called "struct tag" andStackNode_t
is 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.如果您不想在结构中包含“struct stackNode”,我发现我能够做到这一点:
您可以编写这样的代码,并且它编译得很好,而且它运行得很好可能看起来违反直觉。它可能违反直觉,因为我对 struct STACKNODE 和 typedef STACKNODE 使用相同的名称。此外,我在结构体作为定义存在之前对其进行类型定义。它作为定义存在。尽管如此,我发现我可以使用 Microsoft 的 C 语言(至今为止的许多版本)和 Borland C(很久以前)来做到这一点。
我喜欢它,因为如果我已经要对结构进行 typedef,我不喜欢在结构定义中诉诸“struct STACKNODE *nxtptr”(即使用单词“struct”)。
If you do not want to have "struct stackNode" inside the struct, I found that I am able to do this:
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-ing the struct before it 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).
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,您可以将 StackNode 定义为 struct stackNode,并将指针 StackNodePtr 定义为 StackNode。
那么还有什么不清楚的呢?
With typedef, you define StackNode to be struct stackNode, and the Pointer StackNodePtr to be StackNode.
So what is not clear?
好吧,简单地解释一下:Typedef 基本上不做任何其他事情,只是告诉编译器您创建了一个新类型的变量(例如 int、char 等。)
使用 typedef 的主要原因是
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..)
The main reasons why to use typedef are
看看
typedef
是否有帮助。更像是 101。标记为“使用问题”的部分可能会为您的,呃,问题提供一些见解。see if
typedef
helps. More like a 101. The section labelled "Usage concerns" may give some insights into your, er, concerns.