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

后端 未结 6 1810
失恋的感觉
失恋的感觉 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: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};

提交回复
热议问题