const int an_object = 0; const int * const MC_buf = &an_object; const int * buffer = MC_buf;
This code was original using structs, but it is simplified in this example.
It doesn't matter where the const declarations are, C51 refuses to compile it. It seems like it ought to work. In the real deal, I was planning on having groups pointers to arrays in various parts gathered in one location in arrays. But even in this simplified version, it doesn't work. Attempting to do anything like this just results in:
error C247: non-address/-constant initializer
What's more, the exact same thing works in GCC with language c and STRICT ANSI and doesn't produce any errors. In fact, I can dereference MC_buf and get the value of an_object.
It seems like it ought to work.
No, it shouldn't. Initializers of static objects have to be compile-time constants. It comes as a surprise to everyone at some point while learning C, so don't be too upset about it: no, 'const'-qualified variables are not compile-time constants.
What's more, the exact same thing works in GCC with language c and STRICT ANSI and doesn't produce any errors.
Hmm... are sure? I consistently get an error:
c:\temp> gcc -c tt.c tt.c:5: initializer element is not constant
Oh! I was completely zoning out on what that error message was saying and reading it wrong. Ha! I feel stupid now.
Thanks for the wake-up.