c - Initialize array with struct of const data -


my case somehow similar stackoverflow.com/questions/16676973 due lack of knowledge cannot learn essential lesson it...

what want achieve loading constant image data memory @ compile time (without knowing size of each image exactly) , array of identifiers of each image can access them time wish within code. ideal way array of pointers beginnings of each image, there question of how determine exact size of each 1 later on. sizeof(imagedatatype) change depending on when call images[i]... (i guess - no. how?)

how i'm trying looks this:

typedef struct _imagedatatype {     uint8_t imageid;     uint8_t dataofoneimage[] } imagedatatype; const imagedatatype images [] = {     {1,{a, ,l,o,t, ,o,f, ,c,o,n,v,e,r,t,e,d, ,i,m,a,g,e, ,d,a,t,a, ,i,n, ,h,e,x}},      {2,{a, ,l,o,t, ,o,f, ,c,o,n,v,e,r,t,e,d, ,i,m,a,g,e, ,d,a,t,a, ,i,n, ,h,e,x}} //of course, data fake here, illustration purposes, 0x00, 0x0c, etc }; 

of course, throws error, "too many initializers array ...".

taking short - feel have lot of errors there, ideological, said i'm new c i'm trying learn complex things quickly..

please help.

unfortunately n1570 6.7.2.1.18 says:

as special case, last element of structure more 1 named member may have incomplete array type; called flexible array member. in situations, flexible array member ignored. in particular, the size of structure if flexible array member omitted except may have more trailing padding omission imply

this means cannot use method not possible calculate address of single element in array correctly.

you can around separating image data structure:

typedef struct _imagedatatype {     uint8_t imageid;     size_t size;     uint8_t const * data; } imagedatatype; uint8_t const img1data [] = { ... }; uint8_t const img2data [] = { ... }; const imagedatatype images [] = {      { 1, sizeof img1data, img1data },      { 2, sizeof img2data, img2data }, } 

if absolutely want define image data structure, use x-macros around limitation, underlying method still same. x-macros can messy, warned.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -