title: Array of struct pointers and assigning struct data
body: <p>I have run into a problem or maybe I am just doing something wrong since I am new to C and structs.  I want to take a text file such as this:</p>
  <pre><code>3
  Trev,CS,3.5
  Joe,ART,2.5
  Bob,ESC,1.0
  </code></pre>
  <p>and read in the first line as the number of students then from there out gather student info and put them in a struct called StudentData:</p>
  <pre><code>typedef struct StudentData{
     
      char* name;
      char* major;
      double gpa;
    
  } Student;
  </code></pre>
  <p>Where I run into a problem is after I seemingly assign the data to an individual struct, the struct data becomes mixed up.  I have commented out exactly what is going on (or at least what I believe is).  Hopefully it isn't painful to read.</p>
  <pre><code>main(){
      int size, i;
      char* line = malloc(100);
      scanf(&quot;%d\n&quot;, &amp;size);//get size
      char* tok;
      char* temp;
      Student* array[size]; //initialize array of Student pointers
  
      for(i = 0; i&lt;size;i++){
        array[i] = malloc(sizeof(Student));//allocate memory to Student pointed to by array[i]
        array[i]-&gt;name = malloc(50); //allocate memory for Student's name
        array[i]-&gt;major = malloc(30);//allocate memory for Student's major
      }
      
      for(i = 0; i&lt;size;i++){
        scanf(&quot;%s\n&quot;, line);//grab student info and put it in a string
        tok = strtok(line, &quot;,&quot;); //tokenize string, taking name first
        array[i]-&gt;name = tok;//assign name to Student's name attribute
  //    printf(&quot;%s\n&quot;,array[i]-&gt;name);//prints correct value
        line = strtok(NULL, &quot;,&quot;);//tokenize
        array[i]-&gt;major = line;//assign major to Student's major attribute
  //    printf(&quot;%s\n&quot;,array[i]-&gt;major);//prints correct value
        temp = strtok(NULL, &quot;,&quot;);//tokenize
        array[i]-&gt;gpa = atof(temp);//assign gpa to Student's gpa attribute
  //    printf(&quot;%.2f\n\n&quot;,array[i]-&gt;gpa); //prints correct value
      }
      
      for(i=0;i&lt;size;i++){ //this loop is where the data becomes jumbled
        printf(&quot;%s\n&quot;,array[i]-&gt;name);
        printf(&quot;%s\n&quot;,array[i]-&gt;major);
        printf(&quot;%.2f\n\n&quot;,array[i]-&gt;gpa);
    }
  }
  </code></pre>
  <p>The output looks like this:</p>
  <pre><code>Trev
  Joe
  3.50
  
  Joe
  Bob
  2.50
  
  Bob
  ESC
  1.00
  </code></pre>
  <p>I can't really understand what is going on in the memory between assigning values and printing them.</p>