c - 未找到结构体成员

标签 c data-structures struct compiler-errors

我有以下结构:

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

它具有如上所述的三个成员。

我像这样分配顶点:

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’

谁能帮我理解为什么我不能这样做?

最佳答案

Okay so I can do it after the for loop in which I malloced it?

在循环中你会做得更好。

    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;
    }

或者根据 Zeta 的建议:

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

关于c - 未找到结构体成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20109818/

相关文章:

c - C 中的算术运算符和函数调用

c - ARMv8a 中的这种简单分页是如何工作的

data-structures - 为什么代码作为数据?

data-structures - ETS、persistent_term 和流程字典有什么区别?

c - 不是打印 100,而是打印 99.999992

c++ - 将结构从文件写入/读取到 std::vector<>

c - x86 程序集中出现奇怪的段错误

c - OMAP4460 UART 如何实现全双工传输?

Java:过度类型的结构?在 Object[] 中有很多类型?

C结构问题