I just started structures and was interested to implement it to create an adjacency matrix to use in graph related algorithm implementation. So I created a pointer to pointer variable in the graph to use it as the base address for the 2D matrix. But when I tried to assign memory to the array it is showing me an error:
Conversion to non-scalar type requested
Can anyone help me? I am posting the whole code in the following:-
struct graph{
int v;
int e;
struct graph **admat;
};
void main()
{
int x,i,y,z=1,n;
struct graph *G=(struct graph **)malloc(sizeof(struct graph));
printf("\nenter number of vertices: ");
scanf("%d",&G->v);
printf("\nenter number of edges: ");
scanf("%d",&G->e);
G->admat=(struct graph **)malloc(G->v * sizeof(struct graph *));
for(i=0;i<G->v;i++)
{
G[i]=(struct graph)malloc(G->v * sizeof(int));//here is the main error
}
for(x=0;x<i;x++)
{
for(y=0;y<i;y++)
{
G[x][y]=z++;
}
}
for(x=0;x<i;x++)
{
for(y=0;y<i;y++)
{
printf(" %d ",G[x][y]);
}
printf("\n");
}
}