Saturday, 15 May 2010

How can you store a multi-dimensional array in a variable in C? -



How can you store a multi-dimensional array in a variable in C? -

we have next 2 statically-defined byte arrays...

unsigned char pixelbuffer1[32][6] = {/**/}; unsigned char pixelbuffer2[32][6] = {/**/};

we want store current buffer in variable we're not sure how declare it. want do...

[sometype] activepixelbuffer; activebufferid == getoneortwobasedonsomelogic(); activepixelbuffer = (activebufferid == 1) ? pixelbuffer1 : pixelbuffer2; activepixelbuffer[17][4] = x; activebufferid == getoneortwobasedonsomelogic(); activepixelbuffer = (activebufferid == 1) ? pixelbuffer1 : pixelbuffer2; activepixelbuffer[23][2] = y;

(note reassigning value of activepixelbuffer throughout code) however, we're not sure come in [sometype]

i know if passing pixelbuffer1 or pixelbuffer2 function, define this...

void somefunc(unsigned char (&myarray)[32][6]) { ... }

...but doesn't seem work local variable declaration type. also, forces hard-code dimension sizes ok, nice not have that.

we've tried using pointers, 2-dimensional aspect of array throws off too.

so utilize type?

use typedef:

typedef unsigned char pixelbuffer[32][6]; // nb: spot principle, aka dry! pixelbuffer pixelbuffer1 = { ... }; pixelbuffer pixelbuffer2 = { ... }; pixelbuffer *activepixelbuffer = (activebufferid == 1) ? &pixelbuffer1 : &pixelbuffer2; (*activepixelbuffer)[17][4] = x;

note making syntax lot cleaner illustration of spot (single point of truth) principle, aka dry (don't repeat yourself) - 2d array type should defined 1 time - no copying , pasting of unsigned char foo[32][6] !

c arrays types reference

No comments:

Post a Comment