title: Accessing double pointer structures in a function
body: <p>I was trying to use a double pointer structure as array of structures.
  It was working fine when i write the entire code in main().
  Below is the working code:</p>
  
  <pre><code>xkumapu@lxapp-2 cat struct2.c
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;malloc.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;signal.h&gt;
  
  typedef struct conn_s{
      int no;
      char name[32];
  }conn_t;
  int main(){
      int i=0, n;
      conn_t **connections;
      printf("How many students?\n");
      scanf("%d", &amp;n);
      for(i=0;i&lt;n;i++){
  
      connections[i] = (conn_t*)malloc(sizeof(conn_t));
              printf("Enter no,name of student\n");
  
              scanf("%d%s", &amp;connections[i]-&gt;no, &amp;connections[i]-&gt;name);
      }
  
      printf("The student details you entered are");
      for(i=0;i&lt;n;i++){
              printf("%d      %s", connections[i]-&gt;no, connections[i]-&gt;name);
              free(connections[i]);
      }
  
      return 1;
  }
  
  xkumapu@lxapp-2
  xkumapu@lxapp-2 ./struct2
  How many students?
  3
  Enter no,name of student
  1 pavan
  Enter no,name of student
  2 suresh
  Enter no,name of student
  3 ramesh
  The student details you entered are1      pavan2      suresh3      ramesh
  </code></pre>
  
  <p>But, when i use the same code in a function it is not working.</p>
  
  <pre><code>xkumapu@lxapp-2 cp struct2.c  struct1.c
  xkumapu@lxapp-2 vi struct1.c
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;malloc.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;signal.h&gt;
  
  typedef struct conn_s{
      int no;
      char name[32];
  }conn_t;
  int data(){
      int i=0, n;
      conn_t **connections;
      printf("How many students?\n");
      scanf("%d", &amp;n);
      for(i=0;i&lt;n;i++){
  
      connections[i] = (conn_t*)malloc(sizeof(conn_t));
              printf("Enter no,name of student\n");
  
              scanf("%d%s", &amp;connections[i]-&gt;no, &amp;connections[i]-&gt;name);
      }
  
      printf("The student details you entered are");
      for(i=0;i&lt;n;i++){
              printf("%d      %s", connections[i]-&gt;no, connections[i]-&gt;name);
              free(connections[i]);
      }
  
      return 1;
  }
  
  int main(){
      data();
  return 1;
  }
  Entering Ex mode.  Type "visual" to go to Normal mode.
  :wq
  "struct1.c" 41L, 874C written
  xkumapu@lxapp-2 gcc -o struct123 struct1.c
  xkumapu@lxapp-2 ./struct123
  How many students?
  3
  Segmentation fault
  xkumapu@lxapp-2
  </code></pre>
  
  <p>Can you please help me in understanding the problem.</p>