C:在常量数组的维数报告错误 [英] c: const in the dimension of arrays reports errors
本文介绍了C:在常量数组的维数报告错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h>
#include <stdlib.h>
const int N = 5;
int main()
{
int vett[N] = {1, 2, 3, 4, 5};
return 0;
}
什么是code,这部分的问题?编译器报告我这些错误和警告:
What is the problem in this part of code? the compiler report me these error and warnings:
error: variable-sized object may not be initialized
warning: excess elements in array initializer [enabled by default]
warning: (near initialization for 'vett') [enabled by default]
我知道我可以使用定义指令来解决,但我以前在C ++编程,我不想使用改变我的老习惯常量
。也有一些是我能做什么?谢谢你。
I know I can use the define directive to solve but I used to program in c++ and I don't want to change my old habits using const
. There is something I can do? Thanks.
推荐答案
和C ++不同,甚至与 const int的N = 5
, N
不视为C.因此,一个恒定的前pression INT vett [N]
是不是一个正常的(定长)阵列,这是一个可变长度的数组。
Unlike C++, even with const int N = 5
, N
is not considered as a constant expression in C. So int vett[N]
is not a normal (fixed length) array, it's a variable length array.
在这种情况下,你还是应该使用:
In this case, you should still use:
#define N 5
这篇关于C:在常量数组的维数报告错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文