typedef 结构清晰

发布于 2024-10-30 14:49:30 字数 439 浏览 1 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(6

过度放纵 2024-11-06 14:49:30

如果您不想使用 typedef 您始终可以使用完整类型名称:

而不是:

StackNode sn;

您将使用:

struct stackNode sn;

而不是:

StackNodePtr snp;

您将使用:

struct stackNode *snp;

声明完全相同。

If you don't want to use typedef you can always use full type name:

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.

燕归巢 2024-11-06 14:49:30

编写相同内容的更常见方法是:

typedef struct stackNode
{
  int data;
  struct stackNode *nxtptr;

} StackNode_t;

其中 stackNode 是所谓的“结构标记”,StackNode_t 是类型的实际名称。如果您像这样声明结构,则程序的其余部分将不需要关心结构标记,并且您可以使用 nxtptr,就像它是 StackNode_t 一样。

A more common way to write the very same would be:

typedef struct stackNode
{
  int data;
  struct stackNode *nxtptr;

} StackNode_t;

where stackNode is the so called "struct tag" and StackNode_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.

朮生 2024-11-06 14:49:30
typedef struct stackNode
{
  整数数据;
  struct stackNode *nxtptr;

StackNode_t;

如果您不想在结构中包含“struct stackNode”,我发现我能够做到这一点:

typedef struct STACKNODE STACKNODE;
typedef struct STACKNODE
{
  int data;
  STACKNODE *nxtptr;
};

您可以编写这样的代码,并且它编译得很好,而且它运行得很好可能看起来违反直觉。它可能违反直觉,因为我对 struct STACKNODE 和 typedef STACKNODE 使用相同的名称。此外,我在结构体作为定义存在之前对其进行类型定义。它作为定义存在。尽管如此,我发现我可以使用 Microsoft 的 C 语言(至今为止的许多版本)和 Borland C(很久以前)来做到这一点。

我喜欢它,因为如果我已经要对结构进行 typedef,我不喜欢在结构定义中诉诸“struct STACKNODE *nxtptr”(即使用单词“struct”)。

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:

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

遮云壑 2024-11-06 14:49:30

使用 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?

╰つ倒转 2024-11-06 14:49:30

好吧,简单地解释一下:Typedef 基本上不做任何其他事情,只是告诉编译器您创建了一个新类型的变量(例如 int、char 等。)

使用 typedef 的主要原因是

  • 助记符名称。这也经常在库函数中使用。您可以将其类型定义为 size_t,而不是使用 int 或 long 等标准类型。这样这个变量的用途就更清楚了。
  • 缩短名称。通常用于结构之类的东西(这就是你的情况)。为了避免总是必须输入 struct myStruct varname,您可以轻松地使用 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

  • 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.
纸伞微斜 2024-11-06 14:49:30

看看 typedef 是否有帮助。更像是 101。标记为“使用问题”的部分可能会为您的,呃,问题提供一些见解。

see if typedef helps. More like a 101. The section labelled "Usage concerns" may give some insights into your, er, concerns.

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