How to store a a single array from a two dimensional array

I have a two dimensional array initialized as such

uint8_t array2dimension [x][y];

and would like to store a row into a 1 dimension array/

//store a random row into array1dimennsion
int range = x - 0 + 1; 
uint8_t index =  rand() % range + 0;
uint8_t array1dimension[y] = array2dimension[index];

But the code above throws an error

> array must be initialized with a brace-enclosed initializer

This is an invalid array definition/initialization statement. Declare the 1D array elsewhere.

Use memcpy, or a pointer if you don't need to copy it

Here is an example aOob36 - Online C++ Compiler & Debugging Tool -

like this?

int8_t array1dimension[y];
array1dimension[y] = array2dimension[index];

This still throws an error though,

error: invalid array assignment

Post ALL the code, using code tags.

You need to do that with a for loop for each element in the row (or memcpy() if what you call a row is the last dimension of the array)

C++ does not copy subarays for you

If you don’t need the copy, you could get a pointer to the start of the row (array data is guaranteed to be contiguous)

Why? Will your code modify the new 1-D array while the original 2-D array MUST remain unchanged? If not just access the row you need in the original array.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.