Hi, Gurus,
Attempting to use variable to access struct member. Couldn't imagining it's doable by using name reference. But address offset works. The last thing has to be variable is the data type.
Please advise a smarter way to make struct element access variable, various type for casting.Code:typedef struct {char val0;int val1;long val2;} TEST_STRUCT void main(void) {int pOffset;TEST_STRUCT testStruct;testStruct.val0 = 0xAB;testStruct.val1 = 0xDCBA;testStruct.val2 = 0xABCDpOffset = (void *)&testStruct.val2 - (void *)&testStruct.val0; // or use offsetof macroprintf("0x%X.\n", *(long *)(&testStruct.val0 + pOffset); //0xABCD} // end of main
Great thanks,