0

We have the following two statically-defined byte arrays...

unsigned char pixelBuffer1[32][6] = {/**/};
unsigned char pixelBuffer2[32][6] = {/**/};

We want to store the current buffer in a variable but we're not sure how to declare it. This is what we want to 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 we are reassigning the value of activePixelBuffer throughout the code) However, we're not sure what to enter for [SOMETYPE]

I know if we were passing pixelBuffer1 or pixelBuffer2 to a function, we would define it like this...

void someFunc(unsigned char (&myArray)[32][6])
{
    ...
}

...but that doesn't seem to work as a local variable declaration type. Also, that forces us to hard-code the dimension sizes which is ok, but it would be nice not to have to do that.

We've also tried using pointers, but the 2-dimensional aspect of the array throws us off too.

So what do we use for the type?

7
  • 1
    unsigned char **myArray should work. Commented Jun 20, 2014 at 7:01
  • 3
    Use a typedef - it makes life a lot easier for stuff like this. Commented Jun 20, 2014 at 7:02
  • 3
    @LeventeKurusa: no - it really doesn't work - char ** is not equivalent to char[][]. Commented Jun 20, 2014 at 7:03
  • 1
    unsigned char (&myArray)[32][6] Isn't this C++ syntax? Commented Jun 20, 2014 at 7:04
  • Hm, yes I somehow thought they were strings... @MarquelV: you could also try to make it a unsigned char *myArray and then access it with myArray[x + y*WIDTH] Commented Jun 20, 2014 at 7:05

2 Answers 2

7

Use a 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 that as well as making the syntax a lot cleaner this is also an example of the SPOT (Single Point Of Truth) principle, aka DRY (Don't Repeat Yourself) - your 2D array type should only be defined once - no copying and pasting of unsigned char foo[32][6] !

Sign up to request clarification or add additional context in comments.

5 Comments

What's the comment on the first line mean? (i.e. NB: SPOT, etc.) And are you sure to have PixelBuffer1 there and not just PixelBuffer?
Also (new to C/C++) but I thought if you referred to an array without its dimensions, you don't need the address (&) operator since that's what you get back anyway. ...or are you saying you want the address to the variable that points to the array and not the address of the array itself? (I have a feeling its the latter)
@MarqueIV: "And are you sure to have PixelBuffer1 ..." You are correct, Paul R stumbled over the infamous copy/paste programming habbit himself ... ;-) It's been corrected.
@MarqueIV: I added a paragraph explaining SPOT/DRY and why it's a good idea. As for the & dereference - PixelBuffer is typedef'd as a 2D array, and activePixelBuffer is a pointer to a PixelBuffer, hence the need for an & when assigning to the pointer.
@MarqueIV: SPOT is also know as SSOT (en.wikipedia.org/wiki/Single_Source_of_Truth). For completeness here's also an explanation of the DRY principal:en.wikipedia.org/wiki/Don%27t_repeat_yourself
1

Reference to array works in C++:

int activeBufferId = 1;
unsigned char pixelBuffer1[32][6] = {/**/};
unsigned char pixelBuffer2[32][6] = {/**/};
unsigned char (&activePixelBuffer)[32][6] = (activeBufferId == 1) ? pixelBuffer1 : pixelBuffer2;

activePixelBuffer[17][4] = '*';

In C, you have to use pointer:

unsigned char (*activePixelBuffer)[32][6] = (activeBufferId == 1) ? &pixelBuffer1 : &pixelBuffer2;

(*activePixelBuffer)[17][4] = '*';
activePixelBuffer = &pixelBuffer2; // reassign the pointer to point to the other C-array.

Anyway, to ease the syntax and the readability, I suggest to move this array in a struct/class, so you just have to use reference/pointer to the class.

typedef struct PixelData { unsigned char buffer[32][6] } PixelData;

PixelData pixelData1 = {/**/};
PixelData pixelData2 = {/**/};
PixelData *activePixelData = (activeBufferId == 1) ? &pixelData1 : &pixelData2;

4 Comments

Ok, but in your C++ answer, how can you reassign activePixelBuffer later on? (I'm updating my question since by your answer I apparently wasn't clear what we're after.)
As any reference, you can't reassign reference (to point to an other variable). When you want to be able to point to an other variable, you have to use pointer.
Simply do activePixelBuffer = &pixelBuffer2;
I was going to give you the answer for your thoroughness and your last answer, which is almost what we want, but had to give it to the other answer because they too used a typedef but didn't need to use structs to achieve the same thing. Still, just wanted you to know your answer was really good too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.