1

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.

8
  • *out_buffer = &updatedValues; You need to copy the new values to the old array. But really, there's no need for updatedValues at all. Just use out_buffer directly. Commented Nov 23, 2021 at 15:38
  • 2
    Why don't you just sets the values directly to the elements of out_buffer? Commented Nov 23, 2021 at 15:38
  • Either just set the updated values directly to the out_buffer without the detour through updatedValues, or use std::memcpy. Also keep in mind that your function needs to know the array length. Commented Nov 23, 2021 at 15:38
  • You cannot assign a C-style array. (you can copy one using any number of methods, but = 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. Commented Nov 23, 2021 at 15:43
  • 1
    "The above code cannot be modified" Sad, as signature is bad (unsigned int * out_buffer, int size) or unsigned int (& out)[5] or std::span<unsigned int, 5> would be better. Commented Nov 23, 2021 at 15:45

1 Answer 1

3

*out_buffer is an unsigned int.
&updatedValues is an unsigned int(*)[5] - a pointer to an array of five elements - which you can't assign to an int.

You should not assign any arrays (it's impossible), you should modify the contents of the given array:

void ModifyArray(unsigned int *out_buffer) 
{
    out_buffer[0] = 1;
    out_buffer[1] = 2;
    out_buffer[2] = 3;
    out_buffer[3] = 4;
    out_buffer[4] = 5;
}

which you can simplify to

void ModifyArray(unsigned int *out_buffer) 
{
    for (int i = 0; i < 5; i++)
    {
        out_buffer[i] = i+1;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

The first implementation is the one I need. Definitely tried that before, however I may have accidentally run the WRONG test case :D Anyways, it works now! Thank you!

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.