title: How to access elements of a struct in C?
body: <p>I have a struct in my code.
  Structure of struct is:</p>
  
  <pre><code>struct student{
      int Id;
      char Name[30];
      char Subject[30];
  };
  </code></pre>
  
  <p>i want to read a text file containing these information and want to add these information in an array of this struct.</p>
  
  <p>Here is the structure/content of my text file:</p>
  
  <pre><code>1/usman/phd
  2/usman/mphill
  1/usman/abc
  </code></pre>
  
  <p>Here is my code to retrive the values from the text file and save it into the array of struct and then print the values:</p>
  
  <pre><code>void loadStudentData(){
     FILE *fp;
     char buff[255];
     char line[50];
     struct student studentList[20];
     int itemIndex = 0;
     fp = fopen("student.txt", "r");
     while(fgets(line, sizeof line, fp)!=NULL) {
          struct student studentObject;
          int i = 0;
          char *p = strtok (line, "/");
          char *array[3];
          while (p != NULL)
          {
              array[i++] = p;
              p = strtok (NULL, "/");
          }
          printf("\n");
              for (i = 0; i &lt;= 2; ++i){
                  if(i == 0)
                      studentObject.Id =  atoi (array[i]);
                  if(i == 1)
                      strcpy(studentObject.Name ,array[i]);
                  if(i == 2)
                      strcpy(studentObject.Subject, array[i]);
                     // printf("%s", array[i]);
              }
              studentList[itemIndex] = studentObject;
              itemIndex = itemIndex+1;
     }
     fclose(fp);
  
     for(int x=0; x&lt;= sizeof(studentList); x++){
      printf("%s", studentList[x].Name);
     }
  }
  </code></pre>
  
  <p>but this code is not working. can anyone help me to solve this issue?</p>