typedef struct node {...} Node; 到底是做什么的?代表?
What exactly does typedef struct node {...} Node; represent?
请查看此代码块:
typedef struct node
{
int data;
struct node *next;
}
Node;
在此代码中,Node
是 typedef 定义的 struct node
的同义词,还是 node
是 struct 的同义词?如果是后者,那么struct node *next;
是不是就等同于struct struct *next;
?
我是不是把事情复杂化了?
Node
与 struct node
同义。这就是为什么(对于你的例子)而不是使用
struct node* p;
一个可以用
Node* p;
当您使用 typedef
时,您创建了某种类型的 别名 。
所以是的,Node
是 struct node
的别名。
此外,您的代码等同于
struct node
{
int data;
struct node *next;
};
typedef struct node Node;
typedef
不是结构定义的一部分,它是 Node
定义的一部分。
您不再需要到处写 struct
。这不仅可以节省击键次数,还可以使代码更清晰,因为它提供了更多的抽象。
类似
typedef struct {
int x, y;
} Point;
Point point_new(int x, int y)
{
Point a;
a.x = x;
a.y = y;
return a;
}
typedef struct node
{
int data;
struct node *next;
}
Node;
这个可以简单理解
struct node
{
int data;
struct node *next;
};
typedef struct node Node;
struct [structure tag or label] {
member definition;
...
member definition;
} [one or more structure variables];
新变量可以定义为:
struct label <variable>;
或者如果使用typedef struct label 不需要每次都重复定义新的结构体变量
即
typedef struct label Node;
现在可以使用 Node 来定义新的类似类型的变量。
在C语法结构中定义如下
结构或联合说明符:
结构或联合标识符opt { 结构声明列表}
因此要引用此结构说明符,您需要使用其名称。
您可以通过以下方式声明变量
struct node
{
int data;
struct node *next;
} Node;
这里Node
是一个struct node
类型的对象。反过来 struct node
是变量 Node
.
的类型说明符
您可以在结构说明符中省略标识符。在这种情况下,该结构称为 unnamed 结构。但是使用这样的结构你不能在它的定义中引用它本身。例如你不能写
struct
{
int data;
struct *next;
^^^^^^^^^^^^^
} Node;
因为不知道这里指的是什么结构
您可以使用未命名的结构作为其他结构的成员。在这种情况下,这样的结构被命名为 anonymous 结构,其成员成为封闭结构的成员。
例如
struct A
{
struct
{
int x;
int y;
];
int z;
};
这个结构 A
有三个成员 x
、y
和 z
。
当使用 storage-class 说明符时,声明符是一个标识符,它是一个 typedef 名称,表示为标识符指定的类型。
因此在此声明中
typedef struct node
{
int data;
struct node *next;
} Node;
Node
还不是对象。它是一个类型名称,表示结构节点。
所以从现在开始你可以使用类型名称 Node
而不是类型说明符 struct node
请查看此代码块:
typedef struct node
{
int data;
struct node *next;
}
Node;
在此代码中,Node
是 typedef 定义的 struct node
的同义词,还是 node
是 struct 的同义词?如果是后者,那么struct node *next;
是不是就等同于struct struct *next;
?
我是不是把事情复杂化了?
Node
与 struct node
同义。这就是为什么(对于你的例子)而不是使用
struct node* p;
一个可以用
Node* p;
当您使用 typedef
时,您创建了某种类型的 别名 。
所以是的,Node
是 struct node
的别名。
此外,您的代码等同于
struct node
{
int data;
struct node *next;
};
typedef struct node Node;
typedef
不是结构定义的一部分,它是 Node
定义的一部分。
您不再需要到处写 struct
。这不仅可以节省击键次数,还可以使代码更清晰,因为它提供了更多的抽象。
类似
typedef struct {
int x, y;
} Point;
Point point_new(int x, int y)
{
Point a;
a.x = x;
a.y = y;
return a;
}
typedef struct node
{
int data;
struct node *next;
}
Node;
这个可以简单理解
struct node
{
int data;
struct node *next;
};
typedef struct node Node;
struct [structure tag or label] {
member definition;
...
member definition;
} [one or more structure variables];
新变量可以定义为:
struct label <variable>;
或者如果使用typedef struct label 不需要每次都重复定义新的结构体变量 即
typedef struct label Node;
现在可以使用 Node 来定义新的类似类型的变量。
在C语法结构中定义如下
结构或联合说明符:
结构或联合标识符opt { 结构声明列表}
因此要引用此结构说明符,您需要使用其名称。
您可以通过以下方式声明变量
struct node
{
int data;
struct node *next;
} Node;
这里Node
是一个struct node
类型的对象。反过来 struct node
是变量 Node
.
您可以在结构说明符中省略标识符。在这种情况下,该结构称为 unnamed 结构。但是使用这样的结构你不能在它的定义中引用它本身。例如你不能写
struct
{
int data;
struct *next;
^^^^^^^^^^^^^
} Node;
因为不知道这里指的是什么结构
您可以使用未命名的结构作为其他结构的成员。在这种情况下,这样的结构被命名为 anonymous 结构,其成员成为封闭结构的成员。
例如
struct A
{
struct
{
int x;
int y;
];
int z;
};
这个结构 A
有三个成员 x
、y
和 z
。
当使用 storage-class 说明符时,声明符是一个标识符,它是一个 typedef 名称,表示为标识符指定的类型。
因此在此声明中
typedef struct node
{
int data;
struct node *next;
} Node;
Node
还不是对象。它是一个类型名称,表示结构节点。
所以从现在开始你可以使用类型名称 Node
而不是类型说明符 struct node