0

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?

3 Answers 3

2

There is the memcpy function in <string.h>, which is similar to what you implemented as ARRAYCOPY. It copies a block of memory of given size, in your case this size is number of elements in the array * size of an element in the array.

http://www.cplusplus.com/reference/clibrary/cstring/memcpy/

memcpy(destination_array, source_array, number_of_elements * size_of_element);

So, you'd have

// let's allocate exactly as much space as is needed
float* array = (float*)malloc(elements * sizeof(float));

// and now copy the elements we want
switch(elements) {
  case 1: memcpy(array, (float[]){1}, 1 * sizeof(float)); break;
  case 2: memcpy(array, (float[]){1, 2}, 2 * sizeof(float)); break;
  case 3: memcpy(array, (float[]){1, 2, 3}, 3 * sizeof(float)); break;
  case 4: memcpy(array, (float[]){10, 20, 30, 40}, 4 * sizeof(float)); break;
  default:break;
}
Sign up to request clarification or add additional context in comments.

2 Comments

i was using ARRAYCOPY, since memcpy didn't work for me (dunno why)
@umlaeute Sorry, I missed that :) But were you using it right? Does my usage of memcpy work for you?
1

How about:

float *global[] = {
    NULL,
    (float[]){1},
    (float[]){1, 2},
    (float[]){7, 8, 9},
};


/* ... make sure `elements` is in range */
float *array = global[elements];

EDIT

You're obviously free to continue using the switch:

switch(elements) {
case 1:
    array = (float[]){1};
    break;
}

Comments

0

Some options:

1) All of those arrays you may want to use declare on the stack somewhere else, and assign the pointer "array" to whichever one you will actually want. I think this is overly complex though to have these rather pointless arrays floating around.

2) Why doesn't memcpy work? Just remember it thinks in terms of bytes not array elements, so memcpy(target, source, sizeof(source)) should work fine provided target is bigger. I would wrap with if(sizeof(target) > sizeof(source)) { handle error } like this:

case 3:
float temp[] = {1.0,2.0,3.0};
memcpy(array, temp, sizeof(temp));

Make sure you allocate memory (stack or heap) and keep track of the semantic size of array.

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.