Being able to define an array e.g.
int a[] = {1,2,3};
is very convenient, however, the array a is an r-value so I can’t subsequently change the values in a, e.g.
a[] = {4,5,6};
The context for wanting to do this is writing a bunch of unit tests where I am feeding in arrays to functions and testing the outputs. I’m running tests on the same function with different inputs and would like to avoid having to have unique names for my input arrays, e.g. I’m having to do this:
int test1_a[] = {1,2,3};
/* calls to functions */
int test2_a[] = {4,5,6};
/* calls to functions */
Also, if I want to pass a pointer to an array into a function I have to 1st cast it like this:
int a[] = {1,2,3};
int *b = a;
my_func(&b);
passing a pointer to an r-value like this doesn’t work:
my_func(&a);
My question is whether there is any other way to easily initialise an array of values without suffering from these limitations? (particularly with a view to making it easy to write many similar unit tests without each test having a unique set of array names)
If you already have the values you want to pass to the functions, why not use a multi-dimensional array?
Also, if the functions you call expect an array, and you have a e.g.
int a[] = {...}; int *b = a;, then don’t call them with&b. Using&bpasses the address of the pointer, not what it points to.