Dynamic access to components of a struct
Is there in C a way to access fields of a struct in a dynamic way? Look at this pseudo-code:
Code:
typedef struct {
int c1;
int c2;
} t_struct1;
typedef struct {
int c1;
int c2;
int c3;
} t_struct2;
void DumpStruct(const void * s) {
for (int i = 0; i < number_of_vars_in_struct; i++) {
printf("Var %d\n",i);
for (int x=0; x<sizeof(struct_field[i];x++) {
printf("Byte %d = %d\n", x,s->field[i][x]);
}
}
}
main {
t_struct1 p1;
t_struct2 p2;
DumpStruct(&p1);
DumpStruct(&p2);
}