title: How to filter an array of structs by certain parameter?
body: <p>I need to write a program that reads students' details from a file and stores them into an array of structures. After that, I need to filter the data by prompting user. My input file is as below:</p>
  
  <pre><code>Kristina
  Science 30
  Desmond
  Geography 78
  Fred
  Science 87
  Kristina 
  History 45
  Desmond 
  Mathematics 34
  </code></pre>
  
  <p>I declare my struct to store the data as</p>
  
  <pre><code>typedef struct {
      char name[102];
      char subject[40];
      int marks;
  }Student;
  </code></pre>
  
  <p>I need to ask the user whether he wants to see the data by name or by subject. If he chooses name, then he will need to select whose data to be shown. For example, if he chooses the name <code>Kristina</code>, the program should output</p>
  
  <pre><code>Kristina
  Science 30
  History 45
  </code></pre>
  
  <p>I managed to read from file and store the data into array of structs, but I am stuck in writing the function to output the filtered data. My code for the function is</p>
  
  <pre><code>Student* filter_name(char choice[], Student* res, int index)
  {
      int i;
      choice[strcspn(choice,"\n")]=0;
      for(i=0;i&lt;index;i++)
      {
          if (strcmp(choice,res[i].name)==0)
          {
              return res;
          }
      }
  }
  </code></pre>
  
  <p>After I run my code, it is still the same array of structs that was read from the file previously. I need the filtered array to sort it later in my program.</p>
  
  <p>I need a copy of the elements that match, because i will need the existing array for another purpose as well.</p>
  
  <p>Anyone can tell me how to return the filtered array?</p>