I want to use a single pointer pt
and a offset size_t offp
to update the value of either myNFO.valueA
or myNFO.valueB
depends on run time conditions.
I can set pt
to &myNFO
, but I do not know the proper type of pt
and how should I calculate offp
.
Edited:
I forget to mention the structures I used are allocated at runtime.
I am using khash, which need to distinguish whether a key exists. If I use
pt = &pNFO->valueA;
, I have to write similar codes twice. Thus I want to determine which member first and then turn to the hash.
typedef struct {
uint16_t valueA;
uint16_t valueB;
const char * fileName;
} __attribute__ ((__packed__)) Info_t;
Info_t myNFO, *pNFO;
pNFO = calloc(1,sizeof(Info_t));
size_t offp = &(myNFO.valueB) - &(myNFO.valueA);
if (ret==1) {
pt = pNFO;
} else {
pt = pNFO + offp;
}
*pt = 100;