Designated Initializers in C

Designated Initializers in C,You should know

Standard C90 requires the elements of an initializer to appear in a fixed order, the same as the order of the elements in the array or structure being initialized.

In ISO C99 you can give the elements in any order, specifying the array indices or structure field names they apply to, and GNU C allows this as an extension in C90 mode as well. This extension is not implemented in GNU C++.

 

Designated Initializers of an array in C:

The C99 introduces a new mechanism to initialize the elements of the array. It allows you to initialize specific elements of the array in any sequence, you don’t need to initialize the array from the beginning.

In this method, if the size of the array is not given, then the largest initialized position becomes the size of the array (length of the array is the highest value specified plus one) and all uninitialized position initialized with 0.

To specify an array index, write ‘[index] =’ before the element value. For example,

int a[6] = {[4] = 29, [2] = 15 }; 

              or

int a[6] = {[4]29 , [2]15 };

 

The above statement is equivalent to,

int a[6] = { 0, 0, 15, 0, 29, 0 };

 

Note:- The index values must be constant expressions.

 

To initialize a range of elements to the same value, write ‘[first … last] = value’. This is a GNU extension. For example,

int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };

 

Let’s consider an example for better understanding,

I am creating an integer array aiData, whose size is 50 and initializing the elements of the array using their index.

int aiData[20] = { 1, 2, 3, [15] = 40, 5, [13] = 80, [18] = 89 };

In this example, the first three elements are initialized to 1, 2, and 3 respectively. Then index 15 ( 16th element of the array) is initialized to 40 The next element ( 17th ) is initialized to 5. Then indexes 13, 18 ( 14th, 19th elements of the array ) is initialized to 80 and 89 respectively.

#include <stdio.h>
#include <string.h>

//Size of the static array
#define ARRAY_SIZE sizeof(aiData)/sizeof(aiData[0])

int main()
{

    int aiData[20] = { 1, 2, 3, [15] = 40, 5, [13] = 80, [18] = 89 };
    int iLoop = 0;

    printf("Stored elements of the array\n");
    for(iLoop=0; iLoop < ARRAY_SIZE ; iLoop++)
    {
        printf("     aiData[%d] =  %d\n",iLoop, aiData[iLoop]);
    }

    return 0;
}

Output:

c99 designated initializer

 

 

 

 

 

 

 

 

 

Note:

  • Initializers do not need to appear in order.
  • The length of the array is the highest value specified plus one.

 

Designated Initializers of a structure in C:

In a structure initializer, specify the name of a field to initialize with ‘.fieldname =’ or ‘fieldname:’ before the element value.

Suppose Mydata is a structure and members are a and b.

struct Mydata
{
    int a;
    int b;
};

the following initialization,

struct Mydata InfoData = { .b = 2, .a = 3 }; 

                or

struct Mydata InfoData = { b: 2, a: 3 };

The above statement is equivalent to,

struct Mydata InfoData = { 2 , 3 };

 

Let’s consider an example for better understanding,

#include <stdio.h>

// C program to demonstrate designated
// initializers with structures
struct Point
{
    int a, b, c;
};

int main()
{
    // Examples of initialization using
    // designated initialization
    struct Point p1 = {.b = 0, .c = 1, .a = 2};
    struct Point p2 = {.a = 20};

    printf ("p1.a = %d, p1.b = %d, p1.c = %d\n", p1.a, p1.b, p1.c);
    printf ("p2.a = %d", p2.a);

    return 0;
}

Output:

p1.a = 2, p1.b = 0, p1.c = 1
p2.a = 20

 

Designated Initializers of a union in C:

The designated Initializers of union is similar to the structure. Let see an example C program,

#include <stdio.h>

union Data
{
    int a;
};

int main()
{
    //designated initialization of union
    union Data myData = { .a = 4 };

    printf("myData.a= %d\n",myData.a);

    return 0;
}

Output:

myData.a= 4

 

Recommended Post

Reference :
https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html