Is there a way of finding the offset of a member of a structure at compile-time? I wish to create a constant containing the offset of a structure member. In the following code the offsetof()
macro works in the first printf
statement. However, the use in line 10 to declare ofs
generates the error:
"Cannot resolve '->' operator as a constant expression".
Is there any other way of doing it?
struct MyStruct
{
unsigned long lw;
unsigned char c[5];
int i;
int j;
unsigned long last;
};
const int ofs = offsetof(struct MyStruct, i); // This line in error
int main(void)
{
printf("Offset of c = %d.\n", offsetof(struct MyStruct, c) );
printf("Offset of i = %d.\n", ofs );
return 0;
}