title: Dynamically Allocated Memory in Structure Array (in C)
body: <p>I am having trouble figuring out how to dynamically allocate memory to a array structure. I need to use an array of my defined structure so that I can iterate over it to print the info. However, I am also asked to dynamically allocate memory to the array. </p>
  
  <p>The problem is that when I use malloc() in the code (you will see it is commented out) it throws the pointer off from the array index. Is there any way to accomplish dynamically allocating memory while pointing to the array indicies to store the data?  </p>
  
  <p>DISCLAIMER: I am very new to C, if my coding hurts your eyes, I sincerely apologize in advance. Just looking to understand the program rather than quickly finish it. </p>
  
  <p>PROGRAM INSTRUCTIONS:</p>
  
  <p>Write a C program that uses 'struct' to define a structure containing the following Student:</p>
  
  <p>Information:</p>
  
  <ol>
  <li>Initial [character]</li>
  <li>Age [integer]</li>
  <li>Id [integer]</li>
  <li>Grade [character]</li>
  </ol>
  
  <p>Your program should print the above information for 5 students.</p>
  
  <p>Hint: Use array of structures and iterate over the array to print the information.</p>
  
  <p>Create a structure with:</p>
  
  <ol>
  <li>Variable data of type int</li>
  <li>A character pointer called tag. This pointer should point to the address of a string.</li>
  </ol>
  
  <p>Check if memory is available and then dynamically allocate required memory to your structure.</p>
  
  <p>Assign values to your structure variable and print them to console.</p>
  
  <p>Code so far:</p>
  
  <pre><code># include &lt;stdio.h&gt;
  # include &lt;string.h&gt;
  # include &lt;stdlib.h&gt;
  
  int main (void)
  {
      struct student  
      {
          char initial [21];
          int age;
          float id;
          char grade [3];
      } list[5];
  
      struct student * tag = list;
  
      //LINE BELOW IS WHAT I WAS TRYING TO DO; BUT THROWS POINTER OFF OF STRUCT ARRAY
      //tag = ( struct student * ) malloc ( sizeof ( struct student ));
      strcpy(tag-&gt;initial, "KJ");
      tag-&gt;age = 21;
      tag-&gt;id = 1.0;
      strcpy (tag-&gt;grade, "A");
      tag++;
  
      strcpy(tag-&gt;initial, "MJ");
      tag-&gt;age = 55;
      tag-&gt;id = 1.1;
      strcpy (tag-&gt;grade, "B");
      tag++;
  
      strcpy(tag-&gt;initial, "CJ");
      tag-&gt;age = 67;
      tag-&gt;id = 1.2;
      strcpy (tag-&gt;grade, "C");
      tag++;
  
      strcpy(tag-&gt;initial, "SJ");
      tag-&gt;age = 24;
      tag-&gt;id = 1.3;
      strcpy (tag-&gt;grade, "D");
      tag++;
  
      strcpy(tag-&gt;initial, "DJ");
      tag-&gt;age = 27;
      tag-&gt;id = 1.4;
      strcpy (tag-&gt;grade, "F");
      tag++;
  
      int n;
  
      for ( n = 0; n &lt; 5; n++ ) {
              printf ( "%s is %d, id is %f, grade is %s\n", 
                list [ n ].initial, list [ n ].age, list [ n ].id, list [ n ].grade);
              }
  
      return 0;
  
  }
  </code></pre>