Why does the compiler think that this variable is not constant?

This is my code:

int main()
{
 const int LEN = 5;
 int x[LEN];
}

VS10 says:

Error C2057: Expected Constant Expression

error C2466: cannot allocate array of constant size 0

error C2133: 'x': unknown size

I even tried the code on this page and it gives the same problem (I commented on the code that gives the error and uncommented the correct one): http://msdn.microsoft.com/en-us/library/eff825eh%28VS.71% 29.aspx

If I tried to use the crappy compiler, I would think that this is a bug in the compiler, but this is VS2010!

+3
source share
4 answers

, , .c. MS Visual C C99. C89 . const C. , .

AndreyT.

.cpp.

+10

http://msdn.microsoft.com/en-us/library/3ffb821x.aspx, ", const, ", , ++.

, , - , - #define. sje397, , LEN ? , , # ?

: , , ++, , C, .

+2

int main()
{
    enum { LEN = 5 };
    int x[LEN];
}
+2

:

int main()
{
    const int LEN = 5;
    int* LENptr = (int*)(&LEN);
    *LENptr = 10;
    int x[LEN];
}

const ,

+1

Source: https://habr.com/ru/post/1780608/


All Articles