Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array assignment by index while declaration in c language

void fun ()
{
    int i;
    int a[]=
    {
    [0]=3,
    [1]=5
    };
}

Does the above way of a[] array assignment is supported in c language. if yes which c version.
i compiled above code with gcc it works fine.

but i never saw this kind of assignment before.

like image 858
user121986 Avatar asked Jul 23 '13 10:07

user121986


People also ask

How do you declare an index array?

Declaring Arrays:typeName variableName[size]; This declares an array with the specified size, named variableName, of type typeName. The array is indexed from 0 to size-1. The size (in brackets) must be an integer literal or a constant variable.

What is array explain with example of declaration of array in C?

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To create an array, define the data type (like int ) and specify the name of the array followed by square brackets [].

How do you assign values to an array after declaration?

But, the elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial values in braces {}. For example: int foo [5] = { 16, 2, 77, 40, 12071 };

What is array declaration and initialization in C?

Array Declaration by Initializing ElementsAn array can be initialized at the time of its declaration. In this method of array declaration, the compiler will allocate an array of size equal to the number of the array elements. The following syntax can be used to declare and initialize an array at the same time.


1 Answers

This is GCC extension to C89, part of standard in C99, called 'Designated initializer'.

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

like image 115
nothrow Avatar answered Sep 28 '22 04:09

nothrow