[英]Trying to assign a variable as the length of an array in C
I'm trying to get n specific number of values from the user, to do this I went the easy way and asked the user to enter a specific number of values to be entered. 我试图从用户那里获取n个特定数量的值,为此,我采取了一种简单的方法,要求用户输入要输入的特定数量的值。 This amount of values is saved in a variable, for example " x ":
此值的数量保存在变量中,例如“ x ”:
int x;
printf("Enter the number of values to be entered: ");
scanf("%d", &x);
int array[x];
So when I try to run the code I get the following message: 因此,当我尝试运行代码时,会收到以下消息:
Constant Expression Required 需要常量表达式
Why does this happens? 为什么会这样? How could I fix it?
我该如何解决?
int array[x];
x
is not a compile time constant and the compiler is complaining about it. x
不是一个编译时间常数,编译器正在抱怨它。 Variable Length Arrays are part of C99 standard. 可变长度数组是C99标准的一部分。 Looks like your compiler is not adhering to the standard.
看起来您的编译器未遵循该标准。
You can use malloc
to dynamically allocate the array and later free
it. 您可以使用
malloc
动态分配数组,然后free
它。 Or use a compiler that supports VLA. 或使用支持VLA的编译器。
This declaration: 此声明:
int array[x];
is a Variable length array( VLA
) and it is a C99
feature. 是一个可变长度数组(
VLA
) ,它是C99
功能。 Without VLA
support array sizes must be compile time constants. 没有
VLA
支持,数组大小必须是编译时间常数。 This DrDobb's article
on VLA
is a good reference. DrDobb's article
有关VLA
DrDobb's article
是很好的参考。
You mentioned you were using both Borland C++
and Xcode
, as far as I can tell Borland
s last version was 1997 which would mean it is unlikely to support this feature although I can not find much info. 您提到您同时使用了
Borland C++
和Xcode
,据我所知Borland
的最新版本是1997 ,这意味着尽管我找不到很多信息,但不太可能支持此功能。 If you are using a more modern branch of the tool it claims to suporrt C99 so there may be a way to enable support. 如果您使用的是该工具的较新版本,它声称支持C99,因此可能有一种启用支持的方法。
Xcode
as far as I understand can use either clang
or gcc
both of which support VLA
in c99
mode, gcc supports VLA as an extension
in non-C99 mode and in C++ and clang supports this as well in limited cases
Xcode
据我明白可以使用clang
或gcc
两者支持VLA
在c99
模式, gcc supports VLA as an extension
非C99模式和在C ++和clang supports this as well in limited cases
If you need to develop in both compilers you may have to abandon VLA
and use an alternative method for dynamically sized arrays such as malloc
in C and in C++ std::vector
is probably the right choice for many situations. 如果需要在两个编译器中进行开发,则可能不得不放弃
VLA
并使用替代方法来处理动态调整大小的数组,例如C和C ++中的malloc
std::vector
在许多情况下都是正确的选择。
Disclaimer: There is a lot of ambiguity with respect to the difference of the following two terms: "declaration" and "definition". 免责声明: 关于以下两个术语的区别有很多歧义:“声明”和“定义”。 Note the example I use to illustrate the difference if you happen to interpret the ambiguity differently than I do.
请注意,如果您碰巧以与我不同的方式解释歧义,那么我用来说明差异的示例。
For sake of completeness: 为了完整性:
Declaring a variable length array ( VLA ) is entirely valid in C99, as you have done: 声明可变长度数组( VLA )在C99中完全有效,就像您已经做过的那样:
int n;
scanf( "%d", &n );
int arr[ n ];
... the issue is likely with the compiler you are using or the flags you have specified. ...问题可能与您使用的编译器或指定的标志有关。 An alternative approach is to use the memory pool and
malloc
a block of memory sufficiently large ( n * sizeof( int )
) then free
the block of memory when you are done with it. 一种替代方法是使用内存池并
malloc
一个足够大的内存块( n * sizeof( int )
),然后在完成后free
该内存块。
Going back to your original code, it is very important to remark that: only declaring a variable length array is valid, defining one is not. 回到原始代码, 非常重要的一点是要注意:仅声明可变长度数组是有效的, 定义一个数组不是有效的。 Say for example you wanted to declare a variable length array and zero-fill all of the elements.
举例来说,您想声明一个可变长度的数组,并将所有元素归零。 You could not do the following :
您无法执行以下操作 :
int n;
scanf( "%d", &n );
int arr[ n ] = { 0 }; // Invalid.
... as you are defining arr
. ...在定义
arr
。 You would need to do the following: 您需要执行以下操作:
int n;
scanf( "%d", &n );
int arr[ n ]; // Valid.
// Zero-fills the array.
for ( int i = 0; i < n; i++ ) {
arr[ i ] = 0;
}
<!-- language:C -->
/*maybe you can like this*/
int x;
int *p;
printf("Enter the number of value to be Entered:");
scanf("%d", &x);
p = (int *)malloc(sizeof(int) * x);
//p[0 - (x-1)] can be used
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.