Tuesday, 15 February 2011

c - Does anyone know why this array initialization statement won't work? -



c - Does anyone know why this array initialization statement won't work? -

i done piece of code , realized array initialization doesn't work when give size variable containing number , on.. works when set ana actual number in it. why so?

for example:

does not work:

unsigned int width = 5; unsigned int height = 4; const uint8_t img[width*height]={0,1,2,3,4,5,6,8,7,9,6,2,1,1,2,5,3,8,6}; // why not?

works:

unsigned int width = 5; unsigned int height = 4; const uint8_t img[20]={0,1,2,3,4,5,6,8,7,9,6,2,1,1,2,5,3,8,6}; //manually placing 20

should there reason why first 1 doesn't work?

i plan on taking user input width , height so, first 1 should work isn't working of import me.

when use,

unsigned int width = 5; unsigned int height = 4; const uint8_t img[width*height] = {.......};

img treated variable length array.

when use

unsigned int width = 5; unsigned int height = 4; const uint8_t img[20] = {.......};

img treated array of known size.

the c99 draft standard (n1570) forbids initialization of variable length arrays.

6.7.9 initialization

2 type of entity initialized shall array of unknown size or finish object type not variable length array type

c

No comments:

Post a Comment