Pointer to array

Hello,
I need help with this problem:
I need to work with a group of byte. Data are received from PC via Serial. The intention is to have two arrays of byte. While processing data in first array, the second array will be filled via Serial. Second array is ready (filled up) before first array is processed.
I would like to avoid:
if (arrayA_to_be_processed) 'process arrayA' else 'process arrayB'
in ProcessMethod and FillMethod.

So, the best way for me is to have two pointers: pointerX (pointer to arrayA) and pointerY (pointer to arrayB), and work with pointers. When processing of data is done, set pointerX to point to arrayB and pointerY to point to arrayA. So work can continue.

I have got two arrays of bytes:
byte myArrayA[512];
byte myArrayB[512];

My questions:
How to declare pointer to myArrayA ?
How to write to myArrayA via pointer to myArrayA ? like: pointerX[54] = 15;
How to read from myArrayA via pointer to myArrayA ? like byte abc = pointerX[54];

Thanks...
Mira.

How to declare pointer to myArrayA ?

myArrayA effectively is a pointer to myArrayA.

AWOL:
myArrayA effectively is a pointer to myArrayA.

Thus you can make another one point to it by assignment:

char* ptr1 = myArrayA;

aarg:
Thus you can make another one point to it by assignment:

char* ptr1 = myArrayA;

I think I'd prefer a cast to make it clear you're happy going from unsigned to signed.

mira4:
How to write to myArrayA via pointer to myArrayA ? like: pointerX[54] = 15;
How to read from myArrayA via pointer to myArrayA ? like byte abc = pointerX[54];

Actually, that syntax will work. Or:

*(pointerX + 54) = 15;
byte abc = *(pointerX + 54);

Or if you want to freak people out

54[pointerX]= 017;
uint8_t abc = 066[pointerX];
1 Like

Great, thank you. This works (pretty easy):
byte myBuffA[512];
byte myBuffB[512];
byte *pointerX;
byte *pointerY;

void setup()
{
myBuffA[12] = 100;
myBuffB[12] = 200;

pointerX = myBuffA;
pointerY = myBuffB;

pointerX[12] = 55;
pointerY[12] = 255;

pointerX = myBuffB;
pointerY = myBuffA;

Serial.write(pointerX[12]); // 255 is sent ;
Serial.write(myBuffA[12]); //55 is sent;
}

1 Like