this seems to be a simple question, but i cannot get my head around it...
i want to set the elements of an array, based on some conditions, in the most elegant way.
this is my non-compiling pseudo code:
float*array=NULL;
switch(elements) {
case 1: array={1}; break;
case 2: array={7, 1}; break;
case 3: array={3, 2, -1}; break;
case 4: array={10, 20, 30, 40}; break;
default:break;
}
the size of the array is limited, so i could do something like 'float array[16];' as well, but the problem is obviously assignment in the case-statement.
i really don't want to do something like:
case 2: array[0]=1; array[1]=2;
my current (clumsy) implementation is:
#define ARRAYCOPY(dest, src) for(int i=0;i<sizeof(src)/sizeof(*src);i++)dest[i]=src[i]
// ...
case 2: do {float*vec={1, 2}; ARRAYCOPY(array, vec); } while(0); break;
i'm using the ARRAYCOPY define, since memcpy() doesn't seem to work. at least doing
float*vec={1, 2}; memcpy(array, vec, sizeof(vec)/sizeof(*vec);
did not fill any values into array.
i guess there must be a nicer solution?