if(stat("seek.pc.db", &files) ==0 )
sizes=files.st_size;
sizes=sizes/sizeof(int);
int s[sizes];
我正在使用Visual Studio 2008进行编译,但是出现以下错误: error C2057: 期望常量表达式 error C2466: 无法分配大小为0的常量数组。
我尝试使用vector s[sizes],但没有成功。我做错了什么?
谢谢!
if(stat("seek.pc.db", &files) ==0 )
sizes=files.st_size;
sizes=sizes/sizeof(int);
int s[sizes];
我正在使用Visual Studio 2008进行编译,但是出现以下错误: error C2057: 期望常量表达式 error C2466: 无法分配大小为0的常量数组。
我尝试使用vector s[sizes],但没有成功。我做错了什么?
谢谢!
C语言中的数组变量大小必须在编译时确定。如果只有在运行时才知道大小,你将需要手动使用 malloc
分配内存。
new int[size]
不是 C 语法,它是 C++ 语法。 - hmakholm left over Monica数组的大小必须是编译时常量。然而,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.
const int size = 5;
中将大小初始化为变量吗? - Avaint *s=new int[sizes];
分配内存。它给我返回了 System.AccessViolationException 错误。是因为这个吗? - Avanew
没有问题。可能是因为您访问了超出 0 到 sizes - 1 的索引。 - Mahesh