如何在不使用循环的情况下在C中初始化N维数组 [英] How to initialize N dimensional arrays in C without using loop
本文介绍了如何在不使用循环的情况下在C中初始化N维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要初始化一个3 x 3矩阵,其中前两行为0,最后一行为1.我已经声明了2D数组int matrix[3][3]
I want to initalize a 3 x 3 matrix with first two rows as 0's and last row as 1's. I have declared a 2D array int matrix[3][3]
我要初始化它而不使用如下所示的循环
I want to initialize it without using loops as shown below
0 0 0
0 0 0
1 1 1
0 0 0
0 0 0
1 1 1
我也想解决N维数组
推荐答案
int matrix[3][3] = {
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 1, 1, 1 }
};
或者,更紧凑:
int matrix[3][3] = {
[2] = { 1, 1, 1 }
};
只要N
是固定的,该解决方案就会推广到N
.如果N
大,则可以使用mouviciel的答案来回答此问题.
The solution generalizes for N
so long as N
is fixed. If N
is large, you can use mouviciel's answer to this question.
这篇关于如何在不使用循环的情况下在C中初始化N维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文