Hi, I am trying to save a struct members offset into a constants table, for parameterizing their access. The concept is something like:
Code: Select all
// everything is constant
typedef struct {
type1 table1[size1];
...
typen tablen[sizen];
} Tables;
const size_t offsets[] = { offsetof(table1), ... , offsetof(tablen) };
Then we can access with:
Code: Select all
Tables *tables = somewhere;
void *tableX = tables + offsets[X];
But the const only seems to accept direct values, if using things like these it fails with
error: Expecting constant expression:
Code: Select all
(size_t)((Tables*)&0)->tableX
((Tables)&0).tableX
sizeof(type1) <= to get offset of table2
Curiously the method shown first works within the execution code, but not at constant declaration, even if it is a constant value by itself.