title: Create a graph with an adjacency matrix in C
body: <p>I was implementing a graph program based on an adjacency matrix in C.
  But I am getting a segmentation fault while I am initializing the matrix (assigning the value of zero). 
  I am not sure whether I am doing any mistake in accessing double pointers or not. </p>
  
  <p>Can anyone please help me in resolving the issue?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>struct Graph {
      int V;
      int E;
      int **adj;
  };
  
  
  struct Graph *addelements() {
      int i,j,a,u,v;  
  
      struct Graph *G= (struct Graph*)malloc(sizeof(struct Graph*));
      printf("Enter the number of vertices and edges : ");
      scanf("%d %d", &amp;G-&gt;V,&amp;G-&gt;E);;
      printf("%d, %d\n",G-&gt;V ,G-&gt;E);
      //G-&gt;adj = (int **)malloc(sizeof(int **)*(G-&gt;V * G-&gt;E));
      G-&gt;adj = malloc(sizeof(G-&gt;V * G-&gt;E));
  
      //Initialization of vertices
  
      for(i=0;i&lt;=G-&gt;V;i++) {
          for(j=0;i&lt;=G-&gt;V;j++) {
              G-&gt;adj[i][j]=0;
          }
      }
  
  
      //Reading the edges;
      for(i=0;i&lt;G-&gt;E;i++) {
          printf("Enter the source and destination : ");  
          scanf("%d %d\n", &amp;u,&amp;v);       
          G-&gt;adj[u][v]=1;    
          G-&gt;adj[v][u]=1;
      }
  
      //printing the matrix
  
      for(i=0;i&lt; G-&gt;V;i++) {
          for(j=0;i&lt; G-&gt;V;j++) {
              printf("%d", G-&gt;adj[i][j]);4
          }
      }
  
     return G;
  }
  
  
  int main() {  
      struct Graph *a= (struct Graph*)malloc(sizeof(struct Graph*)); 
      a =  addelements();  
  } 
  </code></pre>
  
  <p>The output:</p>
  
  <blockquote>
    <p>Enter the number of vertices and edges : 4 5</p>
    
    <p>Segmentation fault (core dumped)</p>
  </blockquote>