Array Initialization

不常见的C数组初始化方法。

 

1 static int collection[] =
2 {
3 [5] = 100,
4 [1] = 50
5 };

 

C99 新特性之一 Designated initializer. 允许不按照下标顺序初始化数组。


数组大小值可选


写法 1 - 不指定数组大小,数组大小为最大下标+1 

 

static int collection[] =


写法 2 - 指定数组大小,不少于最大下标+1 

1 static int collection[6] =
2 {
3 [5] = 100
4 };


编译器支持

gcc  支持。C89模式的gcc通过GNU extension支持

g++  不支持

VC++  不支持


参考

Designated initializers for aggregate types (C only)

posted on 2011-12-26 15:09  Feilian  阅读(120)  评论(0编辑  收藏  举报

导航