简体   繁体   English

将2D数组初始化为相同的值?

[英]Initialise a 2D array all to the same value?

I was wondering if it's possible in C to initialise every element of a 2D array to the same value. 我想知道在C中是否有可能将2D数组的每个元素初始化为相同的值。 I declare my 2D array like this: 我这样声明我的2D数组:

char arry[x][y];

and I want every element arry[0...x][0...y] to contain the asterisks character. 并且我希望每个元素arry [0 ... x] [0 ... y]都包含星号字符。 Is there any simple way to do this without loops? 有没有简单的方法可以做到无循环? I'm thinking something like: 我在想类似的东西:

char arry[x][y] = '*'

But I know that doesn't work. 但是我知道那行不通。

It is possible as long as "the same value" is zero 只要“相同值”为零是可能的

char arry[x][y] = { 0 };

The above will initialize all elements of the array with zeros. 上面将用零初始化数组的所有元素。

For any other value, it is not possible through initialization syntax (unless you want to spell that "same value" for each array element explicitly). 对于其他任何值,都无法通过初始化语法来实现(除非您要为每个数组元素明确拼写“相同的值”)。 You will either write your own initialization cycle or use a pre-written function. 您将编写自己的初始化周期或使用预先编写的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM