Im developing with Microchips XC8 C compiler 1.12.
I have the following code snippet but the compiler is not happy with it.
struct _foo {
int a;
};
enum BAR {
CONST1 = sizeof(struct _foo)
};
The error I get is: main.c:6: error: integer expression required
Why does it generate that error? Isn't the size of any struct in C constant?
If I use sizeof(int) everything works fine.
Note: the XC8 compiler v1.12 follows the C90 standard, not the modern C99 standard.
PS This does compile:
char abc[sizeof(struct _foo)];
And this expression also needs to be a compile-time constant leading me to think that this actually isn't what's going wrong in the enum declaration.
This looks like a limitation / bug of the Microchip XC8 compiler.
In a enumeration constant definition, the compiler does not consider
sizeof (X)
as constant expression (even if actually it is in c90 or c99) whenX
is either an aggregate or union type, or an object of an aggregate type or union type.