A small piece of code:
void func()
{
const int BUF_SIZE = 5;
char scale[BUF_SIZE];
}
This code is built fine under C++, but under C I have an errors:
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
Why?
Compiler: Microsoft Visual C++ 2008
Thanks in advance!
In C (all variants, I believe), a const
is, ironically, not a constant expression in C. In pre-C99, array lengths must be a constant expression.
However, C99 has the concept of "variable length arrays", so if you're compiling with a C99-compliant compiler, your code should be valid even though BUF_SIZE
is not a constant expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With