Question 2.15: "comp.lang.c FAQ list Question 2.15
Q: How can I access structure fields by name at run time?
A: Keep track of the field offsets as computed using the offsetof() macro (see question 2.14). If structp is a pointer to an instance of the structure, and field f is an int having offset offsetf, f's value can be set indirectly with
*(int *)((char *)structp offsetf) = value;
No comments:
Post a Comment