I wish I could just use the preprocessor, but the input comes at runtime.. I tryed a conditional typedef, doesn't work. Or conditional declaration, doesn't work. Although I didn't really expect them to neither. And since all the code that follows is the exact same, I don't want to have to rewrite it twice.. once for each struct.
Is there a way to do this in C? Or different approach, with same result. All my google searches brought me to C++ templates. If I'm not being clear, maybe this will help:
#include <stdio.h>
struct a32 {
short bits;
unsigned long val;
// more values, not necessarily in the same order
};
struct a64 {
short bits;
unsigned long long val;
// etc...
};
int main(void) {
struct a32 mystruct;
// read mystruct from somewhere
if(mystruct.bits == 64) {
// then I need mystruct to be a64
// re-read mystruct
}
// exact same code for both structs
printf("%d\n", sizeof(mystruct.val));
return 0;
}
Any help would be appreciated.