Reputation: 449
Regarding the code below, is there a way to initialize arr0[]
and arr1[]
to match one of the other two arrays chosen by the input parameter? Or is it possible to make arr0[]
and arr1[]
a constant array? Thanks.
#define LENGTH 4
void foo(char id) {
const char arr_a0[] = {2,1,2,1};
const char arr_a1[] = {4,5,6,7};
const char arr_b0[] = {3,3,3,4};
const char arr_b1[] = {1,5,8,9};
char arr0[LENGTH];
char arr1[LENGTH];
int i;
switch(id) {
case 'a':
for (i = 0; i < LENGTH; ++i) {
arr0[i] = arr_a0[i];
arr1[i] = arr_a1[i];
}
break;
case 'b':
default:
for (i = 0; i < LENGTH; ++i) {
arr0[i] = arr_b0[i];
arr1[i] = arr_b1[i];
}
break;
}
/* Do something with arr0[] and arr1[] */
}
Upvotes: 1
Views: 457
Reputation: 26763
It seems that you do not actually need to initialise two arrays, depending on some condition, from two constant arrays. (This is sometimes called an XY-problem.)
Your question whether the initiliased arrays can be made constant seems to indicate that you might work with two pointers.
Initialise those two pointers to each point to one of a pair of constant arrays.
Without the code using the two "conditionally initialised arrays" or, with the proposed alternative concept, uses the two pointers to constant arrays, this answer cannot give the full code solution for doing it.
I trust that your comment indicates that the basic concept has solved your problem.
Upvotes: 1