I've got two arrays, defined as:

uint8_t array1[10];
uint8_t array2[10];

I'd like to swap the arrays (actually the pointers to each array) using code like this:

uint8_t *tmp = array1;
array1 = array2;
array2 = tmp;

However, I keep getting errors like:

invalid array assignment

for the last two lines. I don't recall this being such a problem in traditional C++. I've tried all sorts of casting to no avail. Is there something I'm missing?

I can get the code to run properly if I switch the declarations for the arrays to use new and have them just be more-or-less pointers rather than arrays, but I'd rather not have to do that:

uint8_t *array1 = new uint8_t[10];
uint8_t *array2 = new uint8_t[10];

Code is running on an ARM-based SAMD51 (Cortex M4 processor).

Thanks for any tips!

1 Answer 1


Figured it out. Though using new works, I wanted to keep the buffers out of the heap. The following declarations work with the swap code:

uint8_t a1[10];
uint8_t a2[10];
uint8_t *array1 = a1;
uint8_t *array2 = a2;

uint8_t *tmp = array1;
array1 = array2;
array2 = tmp;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.