title: Malloc initializing a null pointer
body: <p>Hi I ran in to this situation. I am using malloc to give me an array of 10 pointers. When I see the test pointers in gdb, one of them(the third )points to 0x0. Sometimes the code segfaults when using apple[2]->string = "hello". Why does malloc do this? Thanks in advance for the help.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  
  int
  main(void)
   {
    typedef struct test
      {
        char *string;
        int data;
      } test;
  
     test *apple[10];  // Declare an array of 10 test pointers. This line results in one of  the  pointers having a null value.
     apple[0] = malloc(sizeof(test));
     apple[0]-&gt;string = "hello";
  
     printf("The string is %s\n",apple[0]-&gt;string);
     printf("Size of apple[0]-&gt;data is %d\n",sizeof(apple[0]-&gt;data));
     printf("Size of tester is %d\n",sizeof(test));
     free(apple[0]);
     return 0;
  
   }
  </code></pre>
  
  <p>I wanted to see how the array of pointers would work. I was not intending on using all the 10 pointers. So do I need to malloc only what I need? Is it a coincidence, that the third pointer was 0x0?</p>