Struct (C Programming Language) - Struct Initialization

Struct Initialization

There are two ways to initialize a structure. The C89-style initializers are used when contiguous members may be given.

/* Define a type point to be a struct with integer members x, y */ typedef struct { int x; int y; } point; /* Define a variable p of type point, and initialize all its members inline! */ point p = {1,2};

For non contiguous or out of order members list designated initializer style may be used

/* Define a variable p of type point, and set members using designated initializers*/ point p = {.y = 2, .x = 1};

Omitted elements are initialized to 0. The disadvantage of designated initializer style is that this feature is not defined for C++ programming language, according to C++11 standard.

Read more about this topic:  Struct (C Programming Language)