I'm curious why the following code snippet doesn't compile:
typedef struct Foo {
int a;
int b;
} Foo;
static const Foo FooZero = { 0, 0 };
typedef struct Bar {
Foo foo;
int c;
} Bar;
static const Bar BarZero = { FooZero, 0 };
It complains about the use of FooZero
, stating that FooZero
isn't a Compile-Time Constant
But isn't it? What am I not understanding here?
Obviously, I can simply replace the use of FooZero
in the initializer with { 0, 0 }
- my purpose in asking the question is not how to get around the problem - I'm trying to understand the underlying reason why FooZero
is not, in fact, a compile-time constant.
Thanks
A constant variable is one whose value cannot be updated or altered anywhere in your program. A constant variable must be initialized at its declaration.
When initializing a struct, the first initializer in the list initializes the first declared member (unless a designator is specified) (since C99), and all subsequent initializers without designators (since C99)initialize the struct members declared after the one initialized by the previous expression.
Variables of a struct type can be const, and just like all const variables, they must be initialized. When initializing a struct from a list of values, the initializers are applied to the members in order of declaration.
The const part really applies to the variable, not the structure itself.
It has mainly to do with initialization.
The initialized variables are usually not initialized by code which says "put this value to that location", but by a definition which loads a specific value range, the .data
resp. .rodata
segment, to the memory location where it is supposed to be. This is done by the OS file loader. (Strictly speaking, this is not a property of C, which doesn't know anything about that, but of the execution environment.)
That said, it is not possible to tell a part of this memory area to be copied from another one. But it would be possible that te compiler itselfs recognizes the intent of the declaration and puts the same values to diefferent locations. But that would probably be too much "guessing".
In your case: Wouldn't a pointer to FooZero
maybe a better solution? The values are both the same...
typedef struct Foo {
int a;
int b;
} Foo;
static const Foo FooZero = { 0, 0 };
typedef struct Bar {
Foo * foo;
int c;
} Bar;
static const Bar BarZero = { &FooZero, 0 };
Or the other way round:
typedef struct Foo {
int a;
int b;
} Foo;
typedef struct Bar {
Foo foo;
int c;
} Bar;
static const Bar BarZero = { { 0, 0 }, 0 };
static const Foo * FooZero = &BarZero.foo; // if that is possible, untested...
In the first case, you would have to access BarZero.foo
's components with ->
(like BarZero.foo->a
),
in the second case you would have to access FooZero
's components with ->
(like FooZero->a
).
In the C language a const
or static const
value is not considered a "compile time constant", whereas
#define FooZero {0, 0}
is considered to be a compile-time constant. You may say "But but, it even says const
! How can it not be a constant??" And indeed the language says you cannot change the value of a thing you specify as const
, but you also cannot use it as an initializer. You can ask why all you like, it won't change how the language defines it - although your compiler may give you an option to change this behaviour, and it is not too hard to work around in any case.
I think it is quite common for C compilers to treat static constants like global variables - that is, to actually allocate space for a variable (which never changes), rather than program the hard values into the machine code as it would if you did a #define
. In this case the constants, indeed as the compiler says, are not compile-time constants, although they are constants for all other purposes after they are initialised (at runtime).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With