Designated Initializers in C

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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]);
}

chevron_right


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.
  • 4th to 10th elements have value 10.
  • Then element 80 (the 11th element) is initialized to 10
  • 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. This initializers do not need to appear in order.
  2. The length of the array is the highest value specified plus one.
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}

chevron_right


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 = { 2, 3 };
filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate designated
// initializers with structures
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;
}

chevron_right


Output:

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

We can also combine designated initializers for arrays and structures.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}

chevron_right


Output:

2 0
0 0
6 5
0 0
0 0

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.