I have the following declaration and function call:
unsigned int myArray[5] = {0, 0, 0, 0, 0};
ModifyArray(&myArray[0]);
The above code cannot be modified, it is given as is.
I need to write the implementation for ModifyArray to update myArray to contain 1, 2, 3, 4, 5.
I have written it as:
void ModifyArray(unsigned int * out_buffer)
{
unsigned int updatedValues[5] = {0, 0, 0, 0, 0};
updatedValues[0] = 1;
updatedValues[1] = 2;
updatedValues[2] = 3;
updatedValues[3] = 4;
updatedValues[4] = 5;
*out_buffer = &updatedValues;
}
This doesn't really seem to work. I have a feeling that I'm not doing the assignment correctly on the last line of code, however I have tried multiple variations, and it still doesn't seem to update just element 0 of the array.
What am I doing wrong? Thanks!
P.S.: Please note that the scenario is more complex than presented here. I have simplified for readability purposes, but the code should look very similar to this, just the last assignment should be updated to a correct one if possible.
*out_buffer = &updatedValues;You need to copy the new values to the old array. But really, there's no need forupdatedValuesat all. Just useout_bufferdirectly.out_buffer?out_bufferwithout the detour throughupdatedValues, or usestd::memcpy. Also keep in mind that your function needs to know the array length.=is not one of them). It isn't clear why you need an intermediate array though. "My code is more complex" is not a valid excuse.(unsigned int * out_buffer, int size)orunsigned int (& out)[5]orstd::span<unsigned int, 5>would be better.