C语言 如何在二维数组中使用memset函数来初始化C中的成员?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23039134/
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 11:00:00  来源:igfitidea点击:

How to use memset function in two dimensional array for initialization of members in C?

carraysmemset

提问by Fisol Rasel

I want to know how I can use the memset()function in a two dimensional arrayin C.

我想知道如何在 Cmemset()中的 atwo dimensional array中使用该函数。

I don't want to face any garbage problems in that array. How do I initialize this array?

我不想在该数组中遇到任何垃圾问题。我如何初始化这个数组?

Could someone explain me how to achieve it?

有人可以解释我如何实现它吗?

回答by ajay

If your 2D array has static storage duration, then it is default-initialized to zero, i.e., all members of the array are set to zero.

如果您的二维数组具有静态存储持续时间,则默认初始化为零,即数组的所有成员都设置为零。

If the 2D array has automatic storage duration, then you can use an array initializer list to set all members to zero.

如果二维数组具有自动存储持续时间,那么您可以使用数组初始值设定项列表将所有成员设置为零。

int arr[10][20] = {0};  // easier way
// this does the same
memset(arr, 0, sizeof arr); 

If you allocate your array dynamically, then you can use memsetto set all bytes to zero.

如果动态分配数组,则可以使用memset将所有字节设置为零。

int *arr = malloc((10*20) * (sizeof *arr));
// check arr for NULL

// arr --> pointer to the buffer to be set to 0
// 0 --> value the bytes should be set to
// (10*20*) * (sizeof *arr) --> number of bytes to be set 
memset(arr, 0, (10*20*) * (sizeof *arr));

回答by Alok Save

Simply:

简单地:

int array[10][10]={0};

It will initialize all your array members to 0.

它会将您的所有数组成员初始化为 0。



C99 Standard 6.7.8.21:

C99 标准 6.7.8.21:

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

如果花括号括起来的列表中的初始值设定项少于聚合的元素或成员,或者用于初始化已知大小数组的字符串文字中的字符少于数组中的元素,则聚合的其余部分应隐式初始化与具有静态存储持续时间的对象相同。

回答by M.M

Alok Save's answer is the best one, as it works for any type of array. You can also reset the array later , e.g.:

Alok Save 的答案是最好的答案,因为它适用于任何类型的数组。您也可以稍后重置数组,例如:

{
    T const blank[10][10] = { 0 };
    STATIC_ASSERT(sizeof blank == sizeof array);
    memcpy(&array, &blank, sizeof array);
}

This works for all types.

这适用于所有类型。

If you really want to use memsetthen you can do:

如果你真的想使用,memset那么你可以这样做:

memset(&array, 0, sizeof array);

If you only have a pointer to the first element of the array, then:

如果你只有一个指向数组第一个元素的指针,那么:

memset(ptr, 0, number_of_elements * sizeof *ptr);

However, these versions set all-bytes-zero, which might not be a valid representation for floating-point types and pointer types.

但是,这些版本将所有字节设置为零,这可能不是浮点类型和指针类型的有效表示。