【发布时间】:2020-05-05 20:19:29
【问题描述】:
我有一个图形结构,v 代表节点数,**adjmatrix 是邻接矩阵,我在用 0 初始化所有元素时遇到问题,我在adjmatrix[0][0]=0 遇到分段错误。
结构如下:
struct Graph {
int V;
int **adjmatrix;
};
这是初始化图形的函数:
struct Graph *createGraph(int V) {
struct Graph *graph = (struct Graph *)malloc(sizeof(struct Graph));
graph->V = V;
graph->adjmatrix = (int *)malloc(V * V * sizeof(int));
int i, j;
for (i = 0; i < V; ++i) {
for (j = 0; j < V; j++) {
graph->adjmatrix[i][j] = 0; //here is where i get segmentation fault
}
}
return graph;
}
【问题讨论】:
-
您可以通过点击分数下方的灰色复选标记来接受其中一个答案
标签: c graph malloc adjacency-matrix