Error C2057: expected constant expression

if(stat("seek.pc.db", &files) ==0 ) sizes=files.st_size; sizes=sizes/sizeof(int); int s[sizes]; 

I compile this in Visual Studio 2008, and I get the following error: error C2057: expected constant expression error C2466: cannot allocate an array of constant size 0.

I tried using the vector s [sizes], but to no avail. What am I doing wrong?

Thanks!

+7
source share
2 answers

The sizes of array variables in C must be known at compile time. If you only know this at run time, you will have to use some memory instead of malloc .

+9
source

The size of the array must be a compile time constant. However, C99 supports variable length arrays. So instead of your code working in your environment, if the size of the array is known at runtime, then -

 int *s = malloc(sizes); // .... free s; 

Regarding the error message:

 int a[5]; // ^ 5 is a constant expression int b = 10; int aa[b]; // ^ b is a variable. So, it value can differ at some other point. const int size = 5; int aaa[size]; // size is constant. 
+4
source

All Articles