Initializing Const Struct with other Const Struct Instances

后端 未结 2 1401
天命终不由人
天命终不由人 2021-02-07 22:27

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 s         


        
相关标签:
2条回答
  • 2021-02-07 22:46

    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).

    0 讨论(0)
  • 2021-02-07 22:55

    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).

    0 讨论(0)
提交回复
热议问题