可以(在初始化后)在一行中(重新)设置数组的所有值吗? [英] Can one (re)set all the values of an array in one line (after it has been initialized)?
本文介绍了可以(在初始化后)在一行中(重新)设置数组的所有值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C语言中,我知道我可以创建一个像这样的数组
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;
但是,更像是
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)
推荐答案
memcpy(myarray, (int [5]){a,b,c,d,e}, 5*sizeof(int));
这篇关于可以(在初始化后)在一行中(重新)设置数组的所有值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文