Question 2.15

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


Build a table of names and offsets, using the offsetof() macro. The offset of field b in struct a is

	offsetb = offsetof(struct a, b)

If structp is a pointer to an instance of this structure, and field b is an int (with offset as computed above), b's value can be set indirectly with

	*(int *)((char *)structp + offsetb) = value;


Read sequentially: prev next up top


This page by Steve Summit // Copyright 1995 // mail feedback