0

Is there any way to set the values in an array after it has been declared?

unsigned char values[16];
// Some other code to decide what to put into the values array
values = {0xAA,0xBB,0xCC,0xDD};

I saw this solution but it is for a C++ implementation. I'm trying to be efficient on memory and avoid a memcpy as well as I don't always need to fill the entire array like in the example above. So far this is the only method that I know works but its very kludgy.

unsigned char values[16];
// Some other code to decide what to put into the values array
values[0] = 0xAA;
values[1] = 0xBB;
values[2] = 0xCC;
values[3] = 0xDD;
5
  • 2
    memcpy(values, (int[]){0xAA, 0xBB, 0xCC, 0xDD}, 4*sizeof *values); see ideone.com/9ruF7z Commented Mar 17, 2022 at 20:34
  • 1
    you are efficient when using memcpy()... not when avoiding it :) Commented Mar 17, 2022 at 20:39
  • 1
    @pmg OP's array is an unsigned char array, so a memcpy from an int array won't work. Commented Mar 17, 2022 at 21:14
  • My bad @user3386109... but the point is still valid for unsigned char as long as you change the type of the unnamed object. Thank you! Commented Mar 17, 2022 at 21:18
  • 1
    @pmg Yes sorry, that comment was more for the benefit of the OP, who would be disappointed if they tried it without fully understanding it. I agree that your suggestion is valid. Commented Mar 17, 2022 at 21:22

2 Answers 2

0

Maybe something like this would do:

unsigned char* values;
// Some other code to decide what to put into the values array
values = (unsigned char[]) {0xAA,0xBB,0xCC,0xDD};

Appendix: Note that it really depends on what you need. Since values is not an array anymore in this case.

Sign up to request clarification or add additional context in comments.

2 Comments

This possibly enforces an extra level of indirection though, so this could be less efficient, in case the compiler can't optimize away the values variable.
@Lundin thanks for the hint. I was assuming that any compiler would likely make this to a memcpy, making it equivalent to your answer. Like in the case of, e.g., assigning structs. Maybe I'm confusing or missing something. Could you elaborate what this indirection would be?
0

Either use memcpy or assign each index manually, there is no other sensible solution. You don't have to memcpy the whole array at once, just restrict it to some indices:

memcpy(&values[0], (unsigned char[]){0xAA,0xBB,0xCC,0xDD}, 4);

where values[0] can easily be changed to any values[i]. This is also safe as far as alignment is concerned. Note that there should be no benefit in terms of efficiency between this code and assigning all 4 indices manually. Strive to make the code as readable as possible - if you think compound literals are hard to read for example, then stick to what you already have.

Comments

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.