C语言 将数组设置为一个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4066522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:52:08  来源:igfitidea点击:

Setting an array to one value

c

提问by Devan Buggay

Is there an easier way in Cto set an array to one value than using a forloop and going setting each value one by one?

有没有C比使用for循环并逐个设置每个值更简单的方法来将数组设置为一个值?

回答by Adam Rosenfield

If you're setting the array to all 0's, or if the array is an array of bytes, you can use memset

如果将数组设置为全 0,或者数组是字节数组,则可以使用 memset

// Set myArray to all 0's
memset(myArray, 0, numberOfElementsInMyArray * sizeof(myArray[0]));

If you need to set it to something other than 0 in units larger than a byte (e.g. set an array of ints to 1's), then there is no standard function to do that -- you'll have to write your own for loop for that.

如果您需要以大于一个字节的单位将其设置为 0 以外的其他值(例如将ints数组设置为 1),则没有标准函数可以做到这一点——您必须编写自己的 for 循环那。

回答by nonopolarity

You can set it to the same value, but only to 0

您可以将其设置为相同的值,但仅限于 0

How to initialize all members of an array to the same value?

如何将数组的所有成员初始化为相同的值?

initialize all elements to 0:

将所有元素初始化为0

int myArray[10] = { 0 }; // all elements 0

There is an answer in that page for gcc as well.

该页面中也有 gcc 的答案。

回答by paxdiablo

If it's an array of byte values, or you want to set each byte to a specific value, you can use memset:

如果它是一个字节值数组,或者您想将每个字节设置为特定值,则可以使用memset

char xyzzy[100];
int plugh[40];
memset (xyzzy, 7, sizeof (xyzzy)); // all chars set to 7.
memset (plugh, 0x42, sizeof (plugh));  // all integers set to (e.g.) 0x42424242.

Another possibility is to create a template of the correct size at initialisation time (either real initialisation as per below, or in an initfunction), then call memcpyto blat (a)it onto the real array at a later date.

另一种可能性是创建在初始化时间(无论是实际初始化按以下,或在正确的尺寸的模板init函数),然后调用memcpyBLAT (a)中它到在稍后的日期的实际阵列。

static int template[] = { 1, 1, 1, 1, 1 };
int zorkmid[3];
memcpy (zorkmid, template, sizeof (zorkmid)); // ensure template is at
                                              // least as big as zorkmid.

This latter method is also handy for populating structures with a fixed pre-calculated value: initialise a dummy copy with the required fields set then memcpyit instead of manually setting all the fields each time you want a new instance.

后一种方法对于使用固定的预计算值填充结构也很方便:使用设置的所需字段初始化一个虚拟副本,memcpy而不是每次需要新实例时手动设置所有字段。



(a)Aside:etymology of blat:

(a)旁白: 的词源blat

The Jargon file(see the glossary for all the definitions) lists blatas either the four metasyntactic variable {foo, bar, thud, blat}, or a synonym for blast, sense 1.

行话文件(见所有定义的术语表)列表blat为任何四个metasyntactic变量{foo, bar, thud, blat},或的代名词blast,感觉1。

In turn, blast(sense 1) is defined as a synonym of BLT(that's notthe sandwich), which "referred to any large bit-field copy or move operation".

反过来,blast(sense 1)被定义为BLT(那不是三明治)的同义词,它“指的是任何大的位域复制或移动操作”。

回答by Ben Hymanson

Depends on the value. If it's zero, use memset(a, 0, sizeof(a)). Otherwise, if it's a char array you can use memset with some other value. But memset always works at the char level so if your array is not char only zero is likely to be meaningful.

取决于价值。如果为零,请使用memset(a, 0, sizeof(a)). 否则,如果它是一个字符数组,您可以使用带有其他值的 memset。但是 memset 总是在 char 级别工作,所以如果你的数组不是 char 只有零可能是有意义的。

回答by john

This is an old questions, but here are my two cents.

这是一个老问题,但这是我的两分钱。

I guess that the computer has to iterate over each and every value one way or the other. So why not use a for-loop within a macro? This way your code does not clutter and you get something like a function.

我想计算机必须以一种或另一种方式迭代每个值。那么为什么不在宏中使用 for 循环呢?这样你的代码就不会混乱,你会得到类似函数的东西。

For stack-allocated arrays (with compile-time defined size):

对于堆栈分配的数组(具有编译时定义的大小):

#define fill_array(array, value) \
    for(int i; i < sizeof(array)/sizeof(array[0]); i++) \
    {array[i]=value;}

For heap-alllocated arrays (iter is a pointer to the first entry of the array):

对于堆分配的数组(iter 是指向数组第一个条目的指针):

#define fill_array(iter, length, value) \
    for(int i; i < length; i++) \
    {*iter=value; iter++;}