Interview Questions

How can I access structure fields by name at run time?

C Interview Questions and Answers


(Continued from previous question...)

How can I access structure fields by name at run time?

Keep track of the field offsets as computed using the offsetof() macro . 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;

(Continued on next question...)

Other Interview Questions