If you want to initialize non - static elements in a struct declare :
In C ++ (not C), structs almost synonymous with classes and can have elements initialized in the constructor.
struct s { int i; s(): i(10) { } };
If you want to initialize an instance :
In C or C ++:
struct s { int i; }; ... struct s s_instance = { 10 };
C99 also has a feature called designated initializers:
struct s { int i; }; ... struct s s_instance = { .i = 10, };
There is also a GNU C extension, which is very similar to the initializers assigned by C99, but it is better to use something more portable:
struct s s_instance = { i: 10, };
Alex B Oct 22 '08 at 10:03 2008-10-22 10:03
source share