I am a C starter and learning some data structures of Graph. I read a segement of C code on wikipedia today:
#define MAX_VERTEX_NUM 20
typedef struct ArcNode
{
int adjvex; /* Position of Arc's Vertex */
struct ArcNode *nextarc; /* Pointer to next Arc */
InfoType *info; /* Weight) */
}ArcNode; /* Node */
typedef struct
{
VertexType data; /* Vertex Information */
ArcNode *firstarc; /* Location to the first list node */
}VNode,AdjList[MAX_VERTEX_NUM]; /* Head Node */
typedef struct
{
AdjList vertices;
int vexnum,arcnum; /* Vertex count and Arc count */
GraphKind kind; /* type of Garph, directed, undirected..*/
}ALGraph;`
I read several related post here such as "typedef struct vs struct definitions", but I am still a bit confused on this usage:
typedef struct {.... }VNode,AdjList[MAX_VERTEX_NUM];
So what is AdjList? It is an array? If so, what does this statement mean:
AdjList vertices;
Thanks. Reference:http://zh.wikipedia.org/wiki/%E9%82%BB%E6%8E%A5%E8%A1%A8
AdjList
is an array of size MAX_VERTEX_NUM
of the type defined in the struct
.
Declaration in C are a little funny.
typedef struct {...} AdjList[MAX_VERTEX_NUM]
Should be read as AdjList
is defined as an array of size MAX_VERTEX_NUM
of the type defined in the struct. This is a type, which means you can declare variables to be instances of this type.
AdjList
is a type "array of the type defined in the struct
" of size MAX_VERTEX_NUM
.
A simpler example demonstrating what is going on :
typedef int myintarray[10];
int main()
{
myintarray array; // same as "int array[10]"
array[2] = 2 ;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With