[英]Array initialization at declaration
I have a variable of type int array[10]
. 我有一个类型为int array[10]
的变量。 Is it possible to initialize only the last item of the array? 是否可以仅初始化数组的最后一项?
Yes, using designated initializers (introduced in C99), you can write code like this: 是的,使用指定的初始值设定项(在C99中引入),您可以编写如下代码:
int array[10] = {[9] = 42};
which is equivalent to: 这相当于:
int array[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 42};
This feature is also available in some compiler as an extension, for instance, GCC . 此功能在某些编译器中也可用作扩展名,例如GCC 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.