Designated Initializers in C
Last Updated :
27 Apr, 2022
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
#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:-
- These initializers do not need to appear in order.
- The length of the array is the highest value specified plus one.
C
#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
#include <stdio.h>
struct Point
{
int x, y, z;
};
int main()
{
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
#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
Similar Reads
User-Defined Function in C
A user-defined function is a type of function in C language that is defined by the user himself to perform some specific task. It provides code reusability and modularity to our program. User-defined functions are different from built-in functions as their working is specified by the user and no hea
6 min read
When do we use Initializer List in C++?
Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon. Following is an example that uses the initializer list to initialize x and y of Point class. Example [GFGTABS] C++
8 min read
Order of execution in initializer list in C++
Prerequisite: Classes, Constructors, Initializer list In this article, we will discuss the order of execution in the initializer list in C++. Generally, the order of execution is from top to bottom and left to right. But a rare condition arises where this rule fails is when the initializer list is u
2 min read
Data Types in C
Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc. Example: [GFGTABS] C++ int number; [/GFGTABS]The above statement declares a variable with name number that can store integer values. C is a static
6 min read
Derived Data Types in C
Data types in the C language can be categorized into three types, namely primitive, user-defined, and derived data types. In this article, we shall learn about derived data types. In C, the data types derived from the primitive or built-in data types are called Derived Data Types. In other words, th
4 min read
C Identifiers
In C programming, identifiers are the names used to identify variables, functions, arrays, structures, or any other user-defined items. It is a name that uniquely identifies a program element and can be used to refer to it later in the program. Example: [GFGTABS] C // Creating a variable int val = 1
4 min read
User Defined Data Types in C++
User defined data types are those data types that are defined by the user himself. In C++, these data types allow programmers to extend the basic data types provided and create new types that are more suited to their specific needs. C++ supports 5 user-defined data types: Table of Content ClassStruc
4 min read
C - Pointer to Pointer (Double Pointer)
In C, double pointers are those pointers which stores the address of another pointer. The first pointer is used to store the address of the variable, and the second pointer is used to store the address of the first pointer. That is why they are also known as a pointer to pointer. Let's take a look a
5 min read
Const Qualifier in C
The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed (which depends upon where const variables are stored, we may change the value of the const variable by using a pointer). The result is implementation-defined if an attempt is made to c
6 min read
Types of User Defined Functions in C
A user-defined function is one that is defined by the user when writing any program, as we do not have library functions that have predefined definitions. To meet the specific requirements of the user, the user has to develop his or her own functions. Such functions must be defined properly by the u
4 min read