c - How to do this initialization? -
i have structure, need initialize @ compile time. here current (pseudo)code:
struct { int a; int b; }; struct b { struct а[16][3]; }; #define default {{ \ .a = 1, \ .b = 2, \ }, \ { \ .a = 3, \ .b = 8, \ }, \ { \ .a = 11, \ .b = 29, \ }} #define default2 default, default #define default4 default2, default2 #define default8 default4, default4 #define default16 default8, default8 struct b b = {{default16}};
i don't understand code:
- why need double braces on last line?
moreover, why need double braces in definition of default. understand
{ .a = 3, .b = 8, }
is ordinary structure initialization. second pair of braces seems if initializing b
array of 16
objects of type struct [3]
. why not list values of b
if 1-dimensional array (the same way 1 can access b[20]
)?
thirdly, need add new field
struct b
, have no idea how modify initialization. newstruct b
shall be:struct b { int c[16]; struct a[16][3]; }
compiler arm-none-eabi-gcc 4.7.2 no flags enforce c standard.
if understand correctly,
one brace needed things within struct a
, e.g.
struct a = { .a = 1, .b = 2 };
two braces needed 2 dimensional array, e.g.
int array[2][2] = { { 1, 1 }, { 1, 1 } };
the final brace needed things within struct b
.
finally, add int c[16]
struct b
, can replace last line of snippet following:
struct b b = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{default16}};
or use proper macro substitution readability.
Comments
Post a Comment