Cannot const variables be used in constant expression?

Why is this C code illegal in Visual Studio 2013 Ultimate?

const unsigned int x = 64; char resultBufNative[x+1]; 

It gives error C2057: expected constant expression .


Original question

I am completely puzzled by this.

The corresponding function:

 jstring Java_com_nabto_api_NabtoCApiWrapper_nabtoGetSessionToken(JNIEnv* env, jclass thiz, jobject sessionObj) { const size_t size = 64; char resultBufNative[size+1]; size_t resultLen; //Get the session nabto_handle_t session; session = (nabto_handle_t) (*env)->GetDirectBufferAddress(env, sessionObj); nabto_status_t nabtoStatus = nabtoGetSessionToken(session, resultBufNative, size, &resultLen); if (nabtoStatus == NABTO_OK && resultLen <= size) { // Terminate char array, convert to java string, free buffer and return result resultBufNative[resultLen] = 0; jstring result = (*env)->NewStringUTF(env, resultBufNative); //nabtoFree(resultBufNative); return result; } else { return NULL; } } 

Error message: error C2057: expected constant expression

In the string char resultBufNative[size+1];

For me, this makes the corresponding code as such:

  const size_t size = 64; char resultBufNative[size+1]; 

And apparently const size_t + 1 not a constant expression.

No or no size dicking macros (replacing it with xyzabc gives the same error). size_t defined as typedef unsigned int __w64 size_t when I mouseover size_t in my IDE (Visual Studio Ultimate 2013).

After the expected constant error of expression C2057, the compiler gives 2 more errors:

 C24166: cannot allocate array of size 0 C2133: 'resultBufNative': unknown size 

For the same line of code. Naturally, this is because he does not know what size + 1 , so it becomes equal to 0.

Using

  constexpr const size_t size = 64; char resultBufNative[size+1]; 

Gives error C2065: 'constexpr' : undeclared identifier .

Using

  const size_t size = 65; char resultBufNative[size]; 

Gives error C2057: expected constant expression .

Using

  static const size_t size = 64; char resultBufNative[size+1]; 

Gives error C2057: expected constant expression .

Using

  const unsigned int x = 64; char resultBufNative[x+1]; 

Gives error C2057: expected constant expression .

I guess what I want to do is not supported.

+7
c compiler-errors constant-expression
source share
2 answers

In C89 mode, the size of the array must be a compile time constant, but size not one. const keyword means read only, not compile time constant (in C ++ this is different). So you have a few options:

1) Compile in C99 mode, which supports VLA.

2) Use fixed size arrays:

  char resultBufNative[64+1]; 

3) Use

 #define size 64 

which is basically the same as option (2), except that the preprocessor performs this replacement.

4) Use enum , as suggested by bluepixy, which is a compile-time constant as opposed to const .

+5
source share

The updated solution is to compile C ++ using / Tp in Visual Studio 2015. Although not ideal, the C ++ compiler supports a large subset of C99.

+1
source share

All Articles