title: Segmentation fault while using malloc with char pointers
body: <p>I am new to C and learning structs. I am trying to <code>malloc</code> a char pointer with size 30 but it is giving a segmentation fault(core dump). I searched it on the internet &amp; SO but am not able to resolve this. Any help will be much appreciated.<br>
  Probably I am accessing the <code>char*</code> member of the struct incorrectly ?</p>
  
  <pre><code>typedef struct{
  int x;
  int y;
  char *f;
  char *l;
  }str;
  
  void create_mall();
  
  void create_mall() //Malloc the struct
  {
  str *p;
  p-&gt;f = (char*)malloc(sizeof(char)*30);  // segmentation fault here
  p-&gt;l = (char*)malloc(sizeof(char)*30);
  printf("Enter the user ID:");
  scanf("%d",&amp;p-&gt;x);
  printf("\nEnter the phone number:");
  scanf("%d",&amp;p-&gt;y);
  printf("\nEnter the First name:");
  scanf("%29s",p-&gt;f);
  printf("\nEnter the Last name:");
  scanf("%29s",p-&gt;l);
  printf("\nEntered values are: %d %d %s %s\n",p-&gt;x,p-&gt;y,p-&gt;f,p-&gt;l);
  }
  
  int main(void)
  {
  create_mall();
  return 0;
  }
  </code></pre>