Question

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
}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top