我已经为图邻接矩阵编写了下面的代码,但是得到了segementation错误。我认为这是malloc声明有问题,但我不知道是什么。我怎样才能解决这个问题?图邻接矩阵分段故障
#include<stdio.h>
#include<stdlib.h>
struct Graph
{
int V;
int E;
int **adj;
};
struct Graph *adjMatrix()
{
int i,u,v;
struct Graph *G=(struct Graph*)malloc(sizeof(struct Graph));
printf("Enter vetrices and edge\n");
scanf("%d %d",&G->V,&G->E);
G->adj=malloc(sizeof(1)*((G->V) * (G->V)));
for(u=0;u<(G->V);u++)
{
for(v=0;v<(G->V);v++)
{
G->adj[u][v]=0;
}
}
for(i=0;i<(G->E);i++)
{
printf("Enter source vertces and dest vertix ");
scanf("%d %d",&u,&v);
G->adj[u][v]=1;
G->adj[v][u]=1;
}
return G;
}
int main()
{
struct Graph *T=adjMatrix();
printf("%d",T->adj[0][0]);
return 0;
}
什么是输入? –
5和4.只要我输入这个,就有分段错误。 – RaviTej310
你为什么认为这是malloc声明?你有没有调试过它? –