Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean: "typedef struct{...} VNode,AdjList[20]"

Tags:

c

struct

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

like image 383
Andrinux Avatar asked Mar 17 '23 09:03

Andrinux


2 Answers

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.

like image 134
Benjy Kessler Avatar answered Mar 31 '23 22:03

Benjy Kessler


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 ;
}
like image 42
Jabberwocky Avatar answered Mar 31 '23 21:03

Jabberwocky