Why do I get an error about the initializer not being a constant?

后端 未结 6 1668
失恋的感觉
失恋的感觉 2021-01-17 17:49

I am using the following code.

const int X_ORIGIN = 1233086;             
const int Y_ORIGIN = -4728071;              
const int Z_ORIGIN = 4085704;
const in         


        
相关标签:
6条回答
  • 2021-01-17 18:08

    As an alternative, this would also work in this case:

    enum { X_ORIGIN = 1233086,
           Y_ORIGIN = -4728071,
           Z_ORIGIN = 4085704 };
    
    const int xyzOrigin[] = { X_ORIGIN, Y_ORIGIN, Z_ORIGIN };
    
    int main()
    {
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-17 18:11

    In C language objects with static storage duration has to be initialized with constant expressions or with aggregate initializers containing constant expressions. --Answer of AndreyT

    After reading, You must have the knowledge that NUM_DIMENSIONS, If it has the const-qualification, isn't a constant! Then you can't initializate your array this way.

    For use this code:

    const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};

    You should use: #define NUM_DIMENSIONS 3 or you could just declare without any variable inside the square brackets const int xyzOrigin[] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};

    0 讨论(0)
  • 2021-01-17 18:15

    Often people are mislead by the naming of the keyword const, implying something of a constant value that can't be changed. In C at least, it means readonly. const qualified objects at file scope are not having the proper constness to serve as array initializers.

    As an example for non-constant constness, it is perfectly ok to declare

     const volatile unsigned int milliseconds_since_boot;
    

    being a value that gets updated from outside the compiler's control (think HW register) and that you are not allowed to assign to, i.e. it is readonly.

    0 讨论(0)
  • 2021-01-17 18:16

    You can't do this at global scope in C, only at local scope, i.e. within a function:

    #define NUM_DIMENSIONS 3
    
    const int X_ORIGIN = 1233086;             
    const int Y_ORIGIN = -4728071;              
    const int Z_ORIGIN = 4085704;
    
    const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN}; // FAIL
    
    void foo(void)
    {
        const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN}; // OK
    }
    

    Alternatively you could compile the code as C++ rather than C.

    0 讨论(0)
  • 2021-01-17 18:19

    I'm not a proper programmer ;) but I'd do this:

    #define X_ORIGIN (1233086)
    #define Y_ORIGIN (-4728071)
    #define Z_ORIGIN (4085704)
    const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};
    

    That way it's just a text-substitution. If the compiler still spits the dummy at least you're a step closer to knowing where the issue is.

    0 讨论(0)
  • 2021-01-17 18:19

    As triclosan said:

    main()
    {
        const int X_ORIGIN = 1233086;
        const int Y_ORIGIN = -4728071;
        const int Z_ORIGIN = 4085704;
        const int xyzOrigin[] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};
    }
    

    this works fine...

    or, if you know the dimensions beforehand, this:

    #define DIM 3
    
    main()
    {
        const int X_ORIGIN = 1233086;
        const int Y_ORIGIN = -4728071;
        const int Z_ORIGIN = 4085704;
        const int xyzOrigin[DIM] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};
    }
    
    0 讨论(0)
提交回复
热议问题