I have a structure and what I would like to do is to assign values to its members using a for loop. That way I do not have to use the members name. Because the structure is long and i do not want 20 lines of p_struct->member_name etc. What I have so far is below, but i am not sure if i am going in the right direction.
In header file:
typedef struct {
int x;
char ch;
...
...
}data;
data g_data;
in .c file…
data *p_data;
p_data = &(g_data.x)
for(i=0 till struct_elements) {
*p_data = (some value);
p_data++; //next member
}
This is not valid C. But what would be valid C is to make constant table of the types and offsets of each member, and use that in your loop:
Then you could access the member
xas*(int *)((char *)foo + mystruct_def[0].offset).This is just an example; real world usage would probably be a bit more elaborate…