title: Array pointer to the attribute of an array of struct
body: <p>I have the following struct and main function:</p>
  
  <pre><code>struct myStruct{
      string name;
      int id;
      int group;
  };
  
  int main(){
      myStruct student[5]; // The struct student has an array of 5 elements
      search(student, 1, 33);  // We pass the struct student with all the 5 elements
  }
  </code></pre>
  
  <p>I want to pass a struct to the function search, and then make an array pointer that stores the value of a certain attribute, but of all the arrays of the struct.</p>
  
  <p>*e is pointing to student, with all the arrays(5), so if type equals 1 the pointer is going to point to all the values of the attribute of each array of the structure e  </p>
  
  <pre><code>void search(myStruct *e, int type, int value){
      if (type == 1)  int *ptr[] = e[0]-&gt;id;   //An int pointer because the id is an int
      if (type == 2)  int *ptr[] = e[0]-&gt;group;
      for (int i = 0; i &lt; 5; i++){
          if(*ptr[i] == value){
             cout &lt;&lt; e[i]-&gt;name &lt;&lt; endl;
             cout &lt;&lt; e[i]-&gt;id &lt;&lt; endl;
             cout &lt;&lt; e[i]-&gt;group &lt;&lt; endl;
          }
      }
  }
  </code></pre>
  
  <p>I want <code>*ptr[]</code> pointing to each array of the attribute depending on the parameter passed in type. Ex:</p>
  
  <blockquote>
    <p><code>if ( type == 1 )</code>  </p>
    
    <p><code>ptr[0] = e[0].id;</code><br>
    <code>ptr[1] = e[1].id;</code><br>
    <code>ptr[2] = e[2].id;</code><br>
    <code>ptr[3] = e[3].id;</code><br>
    <code>ptr[4] = e[4].id;</code>  </p>
    
    <p><strong>^Notice that is just the id</strong></p>
    
    <p><code>if ( type == 2 )</code>  </p>
    
    <p><code>ptr[0] = e[0].group;</code><br>
    <code>ptr[1] = e[1].group;</code><br>
    <code>ptr[2] = e[2].group;</code><br>
    <code>ptr[3] = e[3].group;</code><br>
    <code>ptr[4] = e[4].group;</code>  </p>
    
    <p><strong>^Notice that is just the group</strong></p>
  </blockquote>
  
  <p>The problem is that I can't find a way to do that, the real struct in my program has more than just three attributes, it actually has eight, so it will be a waste of code if I do a case for each one.
  Thanks for your help.</p>