creating a graph using Adjacency Matrix

Showing segmentation fault(core dumped)
Please someone help me with this.

#include <stdio.h>
#include <malloc.h>
struct Graph{
	int n ;
	int e;
	int **adj;
}*h;

struct Graph *AdjMatrix(){
	int u,v,i,j;
	struct Graph *G = (struct Graph *)malloc(sizeof(struct Graph));
	if(!G)
	{
		printf("Graph cannot be created\n");
		return 0;
	}
	printf("Enter the number of vertices and Edges\n");
	scanf("%d",&(G->n));
	scanf("%d",&G->e);
	G -> adj = (int **)malloc(sizeof(int)*(G->n * G->n));
	for(i=0;i<G->n;i++){
		for(j=0;j<G->n;j++){
			G->adj[i][j]=0;
		}
	}
	printf("Enter the pairs of nodes making Edges\n");
	for(i=0;i<G->e;i++)
		{
			scanf("%d %d",&u,&v);
			G->adj[u][v]=1;
			G->adj[v][u]=1;
			
		}
	return G;
}
int main(int argc, char *argv[])
{
	h=AdjMatrix();
	return 0;
}

Iā€™d prefer not using pointers in competitive programming problems. They are hard to debug.

Learn about vectors. Gonna help a lot :slight_smile:

1 Like
//