在c语言中声明时按索引分配数组

use*_*986 5 c arrays variable-assignment

void fun ()
{
    int i;
    int a[]=
    {
    [0]=3,
    [1]=5
    };
}
Run Code Online (Sandbox Code Playgroud)

c语言是否支持上述[]数组赋值方式.如果是哪个c版本.
我使用gcc编译上面的代码它工作正常.

但我以前从未见过这种作业.

not*_*row 5

这是C89的GCC扩展,是C99标准的一部分,称为"指定初始化程序".

http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Designated-Inits.html.


Dav*_*eri 5

必须使用gcc -std=c99或以上编译,否则你得到:

warning: x forbids specifying subobject to initialize
Run Code Online (Sandbox Code Playgroud)

GNU C允许它作为C89中的扩展,在-pedantic标志打开时跳过此警告可以使用__extension__

void fun ()
{
    int i;
    __extension__ int a[]=
    {
        [0]=3,
        [1]=5
    };
}
Run Code Online (Sandbox Code Playgroud)