错误C2057:预期的常量表达式

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

我在Visual Studio 2008中编译它,我收到以下错误:错误C2057:预期的常量表达式错误C2466:无法分配常量大小为0的数组。

我尝试使用矢量[尺寸]但无济于事。 我究竟做错了什么?

谢谢!

C中的数组变量的大小必须在编译时知道。 如果你只在运行时知道它,你将不得不自己malloc一些内存。

数组的大小必须是编译时常量。 但是,C99支持可变长度数组。 因此,如果您的代码在您的环境中工作,如果在运行时已知数组的大小,那么 –

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

关于错误消息:

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