【问题标题】:What does it mean: "typedef struct{...} VNode,AdjList[20]"这是什么意思:“typedef struct{...} VNode,AdjList[20]”
【发布时间】:2015-04-18 18:53:35
【问题描述】:

我是一名 C 初学者,正在学习 Graph 的一些数据结构。 我今天在维基百科上阅读了一段 C 代码:

    #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;`

我在这里阅读了几篇相关的帖子,例如“typedef struct vs struct definitions”,但我对这种用法仍然有些困惑:

typedef struct  {....  }VNode,AdjList[MAX_VERTEX_NUM];

那么什么是 AdjList?它是一个数组?如果是这样,这句话是什么意思:

AdjList vertices;

谢谢。 参考:http://zh.wikipedia.org/wiki/%E9%82%BB%E6%8E%A5%E8%A1%A8

【问题讨论】:

    标签: c struct


    【解决方案1】:

    AdjList 是一个大小为MAX_VERTEX_NUM 的数组,其类型在struct 中定义。

    C 中的声明有点滑稽。

    typedef struct {...} AdjList[MAX_VERTEX_NUM]
    

    应将AdjList 定义为结构中定义的类型的大小为MAX_VERTEX_NUM 的数组。这是一种类型,这意味着您可以将变量声明为该类型的实例。

    【讨论】:

    • 如果AdjList是一个数组,为什么会有这样的定义:AdjList vertices;AdjList这里看起来像是一种类型...非常感谢您的快速回复。
    • AdjList 是该结构的另一个名称。就像您可以将整数数组定义为 int[40] 一样,您可以将结构数组定义为 AdjList[40]。
    • 我明白了!谢谢你的解释! :)
    【解决方案2】:

    AdjList 是大小为MAX_VERTEX_NUM 的类型“struct 中定义的类型的数组”。

    一个更简单的例子来说明发生了什么:

    typedef int myintarray[10];
    
    int main()
    {
       myintarray array; // same as "int array[10]"
    
       array[2] = 2 ;
    }
    

    【讨论】:

    • 哦,我明白了!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多