[英]What does it mean to typedef a struct as an array?
The graph adjacency list code in my book is given by:我书中的图邻接表代码由下式给出:
typedef struct vertexNode //vertexNode in an AdjacencyList
{
VertexType data;
EdgeNodeType *firstEdgeNode;
} VertexNode, AdjList[MAXVEX];
AdjList adjList; # adjList is a MAXVEX-size array
I was confused by the last line in the typedef: typedef struct{...} AdjList[MAXVEX]
.我对 typedef 中的最后一行感到困惑:
typedef struct{...} AdjList[MAXVEX]
。
The forms I can understand are:我能理解的形式是:
typedef struct{
...
} VertexNode,
VertexNode AdjList[MAXVEX]; # AdjList is a MAXVEX-size array
or或者
struct{
...
} AdjList[MAXVEX]; # AdjList is a MAXVEX-size array
Grammatically speaking, typedef
is actually a storage class, like static
or extern
, and type alias declarations are read like variable declarations.从语法上讲,
typedef
实际上是一个存储类,就像static
或extern
,类型别名声明读起来像变量声明。 Eg例如
int x;
declares x
to be a variable of type int
, while将
x
声明为int
类型的变量,而
typedef int x;
declares x
to be a type alias meaning int
.将
x
声明为类型别名,表示int
。
Similarly,相似地,
struct vertexNode {
...
} VertexNode;
would declare VertexNode
to be a struct vertexNode
variable, but adding typedef
makes it an alias for struct vertexNode
.会将
VertexNode
声明为struct vertexNode
变量,但添加typedef
使其成为struct vertexNode
的别名。 Note that struct vertexNode { ... }
(the whole thing) is a type, just like int
.请注意,
struct vertexNode { ... }
(整个事物)是一种类型,就像int
一样。 It first defines struct vertexNode
, and then refers to it.它首先定义
struct vertexNode
,然后引用它。
Additionally, array declarations may appear to behave strangely when you use commas:此外,当您使用逗号时,数组声明可能会出现奇怪的行为:
int x, y[5];
declares x
to be an int
, while declaring y
to be an array of 5
int
s.将
x
声明为一个int
,同时将y
声明为一个包含5
int
的数组。 (Functions and pointers are also like this.) There are other questions on this site about it. (函数和指针也是这样。)本站还有其他问题。
Putting everything together, your question looks like this if you take away the typedef
:把所有东西放在一起,如果你
typedef
,你的问题看起来像这样:
struct vertexNode
{
VertexType data;
EdgeNodeType *firstEdgeNode;
} VertexNode, AdjList[MAXVEX];
This would declare the type struct vertexNode
, the variable VertexNode
of type struct vertexNode
, and the array AdjList
of MAXVEX
struct vertexNode
s.这将声明类型
struct vertexNode
,可变VertexNode
类型的struct vertexNode
,并且阵列AdjList
的MAXVEX
struct vertexNode
秒。 Adding the typedef
means that VertexNode
becomes an alias for struct vertexNode
and that AdjList
becomes an alias for an array of MAXVEX
struct vertexNode
s.添加
typedef
意味着VertexNode
成为struct vertexNode
的别名,而AdjList
成为MAXVEX
struct vertexNode
数组的别名。 Personally, I wouldn't recommend writing it like this, but I guess it's concise.就个人而言,我不建议这样写,但我想它很简洁。
This is a sample code.这是一个示例代码。
#include <stdio.h>
typedef char STR[1024];
int main() {
STR a = "1234"; // == char a[1024]
printf( "%s\n", a );
return 0;
}
I wrote a example with data type char
.我写了一个数据类型为
char
的例子。 You can replace it with any class or struct..你可以用任何类或结构替换它..
so.. Your code..所以..你的代码..
AdjList a
is same with VertexNode a[MAXVEX]
AdjList a
与VertexNode a[MAXVEX]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.