造成此错误的原因是在定义数组时使用了变量作为数组的大小,而不是一个常量或字面量。编译器需要知道数组的大小,在编译时能够静态分配其内存。为解决此问题,可以使用常量或定义宏来表示数组的大小,如下所示:
#include
int main() { int num[COUNT];
// 其他操作
return 0;
}
或使用 const 常量:
#include
int main() { const int COUNT = 10; int num[COUNT];
// 其他操作
return 0;
}