【发布时间】:2016-04-29 20:05:07
【问题描述】:
我的结构是:
typedef struct grafo GRAFO;
struct aresta {
int adj;
float peso;
};
struct grafo {
struct aresta **arestas;
};
我无法像这样对矩阵 arestas 进行 malloc:
GRAFO *grafo_aux = (GRAFO*) malloc(sizeof(GRAFO));
grafo_aux->arestas = malloc(num_vert * sizeof(struct aresta*));
什么是正确的代码?谢谢!
【问题讨论】:
-
为什么
arestas中的grafo是双指针?我认为没有必要 -
你怎么知道不对?见minimal reproducible example
-
@BarthyB。 - 你为什么这么说?我们不知道 OP 的问题/解决方案。
-
@BarthyB。在
malloc中使用num_vert表示arestas意味着它可能是一个列表。 -
@Ben 标题说矩阵在我看来更像是一个数组而不是一个列表。为此,您不需要双指针 afaik。但我只是猜测,不应该这样做,我不在讨论范围内。