title: Iterating over same type struct members in C
body: <p>Is it possible to iterate of a C struct, where all members are of same type, using a pointer.  Here's some sample code that does not compile:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct
  {
      int mem1 ;
      int mem2 ;
      int mem3 ;
      int mem4 ;
  } foo ;
  
  void my_func( foo* data )
  {
      int i ;
      int* tmp = data ; // This line is the problem
  
      for( i = 0; i &lt; 4; ++i )
      {
          ++tmp ;
          printf( "%d\n", *tmp ) ;
      }
  }
  
  int main()
  {
      foo my_foo ;
      //
      my_foo.mem1 = 0 ;
      my_foo.mem2 = 1 ;
      my_foo.mem3 = 2 ;
      my_foo.mem4 = 3 ;
      //
      my_func( &amp;my_foo ) ;
      return 0 ;
  }
  </code></pre>
  
  <p>The members of foo should be aligned in memory to be one after another, assuming your compiler/kernel does not try to provide stack protection for buffer overflow.</p>
  
  <p>So my question is:</p>
  
  <p>How would I iterate over members of a C struct that are of the same type.</p>