C – initialize a two dimensional global static array of structs that contain more than one array -
i want initialize 2 dimensional global static array of structures contains arrays in it. following doesn’t seems work.
struct mystuct{ int a; int b; int c[2]; int d[2]; }; static mystuct[2][3] = { {{1,1,{1,1},{1,1}}, {2,2,{2,2},{2,2}}, {3,3,{3,3},{3,3}}}, {{7,7,{7,7},{7,7}}, {8,8,{8,8},{8,8}}, {9,9,{9,9},{9,9}}} };
any suggestions?
thanks
struct mystuct{ int a; int b; int c[2]; int d[2]; }; static struct mystuct test [2][3] = { // | col 0 | | col 1 | | col 2 | /* row 0 */ { {1,1,{1,1},{1,1}}, {2,2,{2,2},{2,2}}, {3,3,{3,3},{3,3}} }, /* row 1 */ { {7,7,{7,7},{7,7}}, {8,8,{8,8},{8,8}}, {9,9,{9,9},{9,9}} } };
your matrix declaration has use struct type, i.e struct mystuct test
just test:
#include <stdio.h> int main (void) { struct mystuct{ int a; int b; int c[2]; int d[2]; }; struct mystuct test [2][3] = { // | col 0 | col 1 | col 2 | /* row 0 */ { {1,1,{1,1},{1,1}}, {2,2,{2,2},{2,2}}, {3,3,{3,3},{3,3}} }, /* row 1 */ { {7,7,{7,7},{7,7}}, {8,8,{8,8},{8,8}}, {9,9,{9,9},{9,9}} } }; (size_t i=0; i< 2; i++) { (size_t j=0; j<3; j++) { printf("test[%zu][%zu].a = %d\n", i, j, test[i][j].a); printf("test[%zu][%zu].b = %d\n", i, j, test[i][j].b); (size_t z=0; z<sizeof(test[i][j].c)/sizeof(test[i][j].c[0]); z++) { printf("test[%zu][%zu].c[%zu] = %d\n", i, j, z, test[i][j].c[z]); } (size_t z=0; z<sizeof(test[i][j].c)/sizeof(test[i][j].c[0]); z++) { printf("test[%zu][%zu].d[%zu] = %d\n", i, j, z, test[i][j].c[z]); } printf("\n"); } } return 0; }
Comments
Post a Comment