title: Populating string in an array of pointers to structs
body: <p>I'm reviewing pointers in C and am creating some practice exercises for myself.  I thought it would be good to read in some structs via an array of struct pointers, then display and sort the array.  The problem I'm having is, I feel, rather basic.  The below code reads in the first entry, however the firstName string is always null.  After printing the first struct and continuing the second it goes 'boom' at the time to read into the firstName string.  I thought I initialized the char strings correctly in initParty(), so I'm not sure what the problem is here.  Any ideas?  </p>
  
  <p>Sample run:</p>
  
  <p>*(gdb) run
  Starting program: /home/remnux/C/struct 
  Enter the firstname: john 
  Enter the lastname: smith
  Enter the ssn: 1234
  (null) smith, 1234</p>
  
  <p>Program received signal SIGSEGV, Segmentation fault.
  0x00000000004009a4 in getParty (p=0x0) at struct.c:51
  51    scanf("%s", p->firstName);
  (gdb)* </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct person{
      char *firstName;
      char *lastName;
      int ssn;
    };  
  
  void print(struct person* p){ 
    printf("\n%s ", p-&gt;firstName);
    printf("%s, ", p-&gt;lastName);
    printf("%d\n ", p-&gt;ssn);
  }
  
  bool isGreaterThan(struct person *p1, struct person *p2){
    return (strcmp(p1-&gt;firstName &gt; p2-&gt;firstName) &gt; 0)
      || ((strcmp(p1-&gt;firstName &gt; p2-&gt;firstName) ==  0) &amp;&amp; (strcmp(p1-&gt;lastName &gt; p2-&gt;lastName) &gt; 0)) 
      || ((strcmp(p1-&gt;firstName &gt; p2-&gt;firstName) ==  0) &amp;&amp; (strcmp(p1-&gt;lastName &gt; p2-&gt;lastName) == 0)) &amp;&amp; (p1-&gt;ssn - p2-&gt;ssn &gt; 0); 
  }
  
  void printArray(struct person** parray, int size){
    int i;
    for(i = 0; i &lt; size; i++)
      printf("%s %s, %d", parray[i]-&gt;firstName, parray[i]-&gt;lastName, parray[i]-&gt;ssn);
  }
  
  void sort(struct person** parray, int size){
    int i, j;
    for(i = 0; i &lt; size; i++)
      for(j = 0; j &lt; size-1; j++){
        if(isGreaterThan(parray[j], parray[j+1])){
          struct person *temp = parray[j];
          parray[j] = parray[j+1];
          parray[j+1] = temp;
        }   
      }    
  return;
  }
  
  void initParty(struct person *p){
      p = (struct person*)malloc(sizeof(struct person));
      p-&gt;firstName = (char*)malloc(100 * sizeof(char));
      p-&gt;lastName = (char*)malloc(100 * sizeof(char));
  }
  
  void getParty(struct person *p){
    int ch;
    printf("\nEnter the firstname: ");
    scanf("%s", p-&gt;firstName);
    while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
    printf("\nEnter the lastname: ");
    scanf("%s", p-&gt;lastName);
    while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
    printf("\nEnter the ssn: ");
    scanf("%d", &amp;(p-&gt;ssn));
    while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
  }
  
  int main(){
    int i, ch, size = 2;
  //  printf("Input the number of people you wish to enter: ");
  //  scanf("%d", &amp;size);
  //  while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
    struct person *parray[size];
    for(i = 0; i &lt; size; i++){
      initParty(parray[i]);
      getParty(parray[i]);
      print(parray[i]);
    }
    sort(parray, size);
    printArray(parray, size);
  }
  </code></pre>
  
  <p><strong><em>Edit, after eliminating the warnings when compiling with -Wall:</em></strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  struct person{
      char *firstName;
      char *lastName;
      int ssn;
    };  
  
  void print(struct person* p){ 
    printf("\n%s ", p-&gt;firstName);
    printf("%s, ", p-&gt;lastName);
    printf("%d\n ", p-&gt;ssn);
  }
  
  /*bool isGreaterThan(struct person *p1, struct person *p2){
    return ((strcmp(p1-&gt;firstName &gt; p2-&gt;firstName) &gt; 0)
      || ((strcmp(p1-&gt;firstName &gt; p2-&gt;firstName) ==  0) &amp;&amp; (strcmp(p1-&gt;lastName &gt; p2-&gt;lastName) &gt; 0))
      || ((strcmp(p1-&gt;firstName &gt; p2-&gt;firstName) ==  0) &amp;&amp; (strcmp(p1-&gt;lastName &gt; p2-&gt;lastName) == 0) &amp;&amp; (p1-&gt;ssn - p2-&gt;ssn &gt; 0))); 
  }*/
  
  void printArray(struct person** parray, int size){
    int i;
    for(i = 0; i &lt; size; i++)
      printf("%s %s, %d", parray[i]-&gt;firstName, parray[i]-&gt;lastName, parray[i]-&gt;ssn);
  }
  
  /*void sort(struct person** parray, int size){
    int i, j;
    for(i = 0; i &lt; size; i++)
      for(j = 0; j &lt; size-1; j++){
        if(isGreaterThan(parray[j], parray[j+1])){
          struct person *temp = parray[j];
          parray[j] = parray[j+1];
          parray[j+1] = temp;
        }
   }    
    return;
  } */
  
  void initParty(struct person *p){
      p = (struct person*)malloc(sizeof(struct person));
      p-&gt;firstName = (char*)malloc(100 * sizeof(char));
      p-&gt;lastName = (char*)malloc(100 * sizeof(char));
  }
  
  void getParty(struct person *p){
    int ch;
    printf("\nEnter the firstname: ");
    scanf("%s", p-&gt;firstName);
    while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
    printf("\nEnter the lastname: ");
    scanf("%s", p-&gt;lastName);
    while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
    printf("\nEnter the ssn: ");
    scanf("%d", &amp;(p-&gt;ssn));
    while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
  }
  
  int main(){
    int i, size = 2;
  //  printf("Input the number of people you wish to enter: ");
  //  scanf("%d", &amp;size);
  //  while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
    struct person *parray[size];
    for(i = 0; i &lt; size; i++){
      initParty(parray[i]);
      getParty(parray[i]);
      print(parray[i]);
    }
    //sort(parray, size);
    printArray(parray, size);
    return 0;
   }    
    return;
  } */
  
  void initParty(struct person *p){
      p = (struct person*)malloc(sizeof(struct person));
      p-&gt;firstName = (char*)malloc(100 * sizeof(char));
      p-&gt;lastName = (char*)malloc(100 * sizeof(char));
  }
  
  void getParty(struct person *p){
    int ch;
    printf("\nEnter the firstname: ");
    scanf("%s", p-&gt;firstName);
    while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
    printf("\nEnter the lastname: ");
    scanf("%s", p-&gt;lastName);
    while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
    printf("\nEnter the ssn: ");
    scanf("%d", &amp;(p-&gt;ssn));
    while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
  }
  
  int main(){
    int i, size = 2;
  //  printf("Input the number of people you wish to enter: ");
  //  scanf("%d", &amp;size);
  //  while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
    struct person *parray[size];
    for(i = 0; i &lt; size; i++){
      initParty(parray[i]);
      getParty(parray[i]);
      print(parray[i]);
    }
    //sort(parray, size);
    printArray(parray, size);
    return 0;
   }    
    return;
  } */
  
  void initParty(struct person *p){
      p = (struct person*)malloc(sizeof(struct person));
      p-&gt;firstName = (char*)malloc(100 * sizeof(char));
      p-&gt;lastName = (char*)malloc(100 * sizeof(char));
  }
  
  void getParty(struct person *p){
    int ch;
    printf("\nEnter the firstname: ");
    scanf("%s", p-&gt;firstName);
    while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
    printf("\nEnter the lastname: ");
    scanf("%s", p-&gt;lastName);
    while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
    printf("\nEnter the ssn: ");
    scanf("%d", &amp;(p-&gt;ssn));
    while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
  }
  
  int main(){
    int i, size = 2;
  //  printf("Input the number of people you wish to enter: ");
  //  scanf("%d", &amp;size);
  //  while((ch = getchar()) != '\n' &amp;&amp; ch != EOF);
    struct person *parray[size];
    for(i = 0; i &lt; size; i++){
      initParty(parray[i]);
      getParty(parray[i]);
      print(parray[i]);
    }
    //sort(parray, size);
    printArray(parray, size);
    return 0;
  }
  </code></pre>