Ava*_*Ava 9 c vector visual-studio-2008
if(stat("seek.pc.db", &files) ==0 )
sizes=files.st_size;
sizes=sizes/sizeof(int);
int s[sizes];
Run Code Online (Sandbox Code Playgroud)
我在Visual Studio 2008中编译它,我收到以下错误:错误C2057:预期的常量表达式错误C2466:无法分配常量大小为0的数组.
我尝试使用矢量[尺寸]但无济于事.我究竟做错了什么?
谢谢!
Hen*_*olm 10
C中的数组变量的大小必须在编译时知道.如果你只是在运行时才知道它,你将不得不malloc
自己记忆.
数组的大小必须是编译时常量.但是,C99支持可变长度数组.因此,如果您的代码在您的环境中工作,如果在运行时知道数组的大小,那么 -
int *s = malloc(sizes);
// ....
free s;
Run Code Online (Sandbox Code Playgroud)
关于错误消息:
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.
Run Code Online (Sandbox Code Playgroud)