Open In App

Designated Initializers in C

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 random 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++.
To specify an array index, write ‘[index] =’ or ‘[index]’ before the element value. For example, 
 

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

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’. For example, 
 

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

Source : gcc.gnu.org
 

C




// C program to demonstrate designated initializers
// with arrays.
#include <stdio.h>
void main(void)
{
    int numbers[100] = {1, 2, 3, [3 ... 9] = 10,
          [10] = 80, 15, [70] = 50, [42] = 400 };
 
    int i;
    for (i = 0; i < 20; i++)   
        printf("%d ", numbers[i]);
     
    printf("\n%d ", numbers[70]);
    printf("%d", numbers[42]);
}


Output: 
 

1 2 3 10 10 10 10 10 10 10 80 15 0 0 0 0 0 0 0 0 
50 400 

Explanation: 
 

  • In this example, the first three elements are initialized to 1, 2, and 3 respectively.
  • The 4th to 10th elements have a value of 10.
  • Then the 11th element is initialized to 80.
  • The next element (12th) is initialized to 15.
  • Element number 70 ( the 71st ) is initialized to 50, and number 42 ( the 43rd ) to 400.
  • As with Normal initialization, all uninitialized values are set to zero.

Note:-

  1. These initializers do not need to appear in order.
  2. The length of the array is the highest value specified plus one.

 

C




// C program to demonstrate designated initializers
// to determine size of array.
#include <stdio.h>
 
void main(void)
{
    int numbers[] = {1, 2, 3, [10] = 80, 15,
                    [70] = 50, [42] = 400 };
    int n = sizeof(numbers) / sizeof(numbers[0]);
    printf("%d", n);
}


Output: 
 

71

Explanation: 
If the size of the array is not given, then the largest initialized position determines the size of the array. 
In structure or union: 
In a structure initializer, specify the name of a field to initialize with ‘.fieldname =’ or ‘fieldname:’ before the element value. For example, given the following structure, 
 

     struct point { int x, y; };

the following initialization 
 

     struct point p = { .y = 2, .x = 3 }; or
     struct point p = { y: 2, x: 3 };

is equivalent to 
 

     struct point p = { 3, 2 };

 

C




// C program to demonstrate designated
// initializers with structures
#include <stdio.h>
struct Point
{
    int x, y, z;
};
 
int main()
{
     
    // Examples of initialization using
    // designated initialization
    struct Point p1 = {.y = 0, .z = 1, .x = 2};
    struct Point p2 = {.x = 20};
     
    printf("x = %d, y = %d, z = %d\n",
          p1.x, p1.y, p1.z);
           
    printf("x = %d", p2.x);
     
    return 0;
}


Output: 

x = 2, y = 0, z = 1
x = 20

We can also combine designated initializers for arrays and structures.
 

C




// C program to demonstrate designated initializers
// with structures and arrays combined
#include <stdio.h>
void main(void)
{
    struct point { int x, y; };
    struct point pts[5] = { [2].y = 5, [2].x = 6, [0].x = 2 };
    int i;
    for (i = 0; i < 5; i++)   
        printf("%d %d\n", pts[i].x ,pts[i].y);
}


Output: 
 

2 0
0 0
6 5
0 0
0 0

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



Last Updated : 27 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments