找不到struct成员

时间:2013-11-21 00:06:10

标签: c data-structures struct compiler-errors

我有以下结构:

typedef struct vertex_tag{
    int visited = 0;
    int weight = FLT_MAX;
    int prev;
}vertex_t;

如上所示,它有三个成员。

我像这样malloc顶点:

vertex_t * vertex[G->vertices];
    for(i=0; i < G->vertices; i++)
    {
        vertex[i] = (vertex_t*)malloc(sizeof(vertex_t));
    }

所以我从结构中创建一个矩阵。然后我在我创建的函数中调用它们:

vertex[i]->visited
vertex[i]->weight
vertex[i]->prev

我一直收到以下错误:

error: ‘vertex_t’ has no member named ‘visited’
error: ‘vertex_t’ has no member named ‘weight’
error: ‘vertex_t’ has no member named ‘prev’

任何人都可以帮助我理解为什么我不能这样做吗?

1 个答案:

答案 0 :(得分:1)

  

好的,所以我可以在我使用malloced的for循环之后做到这一点吗?

你在循环中做得更好。

    vertex_t *vertex[G->vertices];
    for (i = 0; i < G->vertices; i++)
    {
        vertex[i] = malloc(sizeof(vertex_t));
        vertex[i]->visited = 0;
        vertex[i]->weight = FLT_MAX;
    }

或根据泽塔的建议:

    vertex_t vertex[G->vertices];
    for (i = 0; i < G->vertices; i++)
    {
        vertex[i].visited = 0;
        vertex[i].weight = FLT_MAX;
    }