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:
#include <stdio.h>
#include <stdlib.h>
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 < 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( &my_foo ) ;
return 0 ;
}
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.
So my question is:
How would I iterate over members of a C struct that are of the same type.