3

Is there a way to set all values of an array after initialization with a single line of code?

For example, I can do this: int array[4] = { 1, 2, 3, 4 }.

However, later on I cannot simply write: array = {4, 3, 2, 1}; to set the array to something else.

Would definitely guess cause it does not know that {4, 3, 2, 1} is an array of ints, but then how do I tell it that it is?

I have found workarounds like:

int tempArray[4] = {4, 3, 2, 1};
array = tempArray[4];

or

array[0] = 4;
array[1] = 3;
array[2] = 2;
array[3] = 1;

However I would definitely prefer not to have to use those workarounds, but instead have something like array = int[4] {4, 3, 2, 1};.

Pink Wolf
  • 76
  • 1
  • 7

1 Answers1

0

Based on this answer, it doesn't seem possible currently. The alternative is to use a static const array (which is very similar to your tempArray workaround), e.g.:

static const int constArray[4] = { 1, 2, 3, 4 };

array = constArray;