Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one (re)set all the values of an array in one line (after it has been initialized)?

In C, I know I can make an array like this

int myarray[5] = {a,b,c,d,e};

However, imagine the array was already initialised like

int myarray[5];

and then at some point afterwards, I wanted to set/change all the values without going

myarray[0] = a;
myarray[1] = b;
myarray[2] = c;
myarray[3] = d;
myarray[4] = e;

but rather, something more like

myarray = {a,b,c,d,e};

The reason why I ask this is because if I declare my array on the heap, I will initialise the array like:

int* myarray = malloc(5*sizeof(int));

Then I would like to be able to enter in all the values in one line (mostly to make my code look cleaner)

like image 644
tom Avatar asked Nov 27 '10 22:11

tom


1 Answers

memcpy(myarray, (int [5]){a,b,c,d,e}, 5*sizeof(int));
like image 169
R.. GitHub STOP HELPING ICE Avatar answered Oct 11 '22 19:10

R.. GitHub STOP HELPING ICE