[英]Why do we need to initialize or declare two dimensions simultaneously and not one after another in c
int a[3][3] = { {1, 2, 3} , {4, 5, 6} , {7, 8, 9} };
Is working 正在工作
int a[3][3] ;
a[3][3] = { {1, 2, 3} , {4, 5, 6} , {7, 8, 9}
Is not working . 不管用 。 It shows and error in my gcc compiler
它在我的gcc编译器中显示和错误
The first line of code is declaring the array and defining the entire contents . 第一行代码是声明数组并定义整个内容 。 What you're saying here is "create a 3x3 array of integers with these contents."
你在这里说的是“用这些内容创建一个3x3的整数数组。”
When you write: 当你写:
int a[3][3];
What you're saying is, "create a 3x3 array of integers." 你所说的是,“创建一个3x3的整数数组。” You're not specifying the contents.
你没有指定内容。
Your second example has two problems. 你的第二个例子有两个问题。
First, arrays are 0-based, so a[3][3]
doesn't exist. 首先,数组是从0开始的,所以
a[3][3]
不存在。 The valid indexes are 0, 1, and 2. 有效索引为0,1和2。
More importantly, when you address a[x][y]
, you're addressing that specific cell . 更重要的是,当您解决
a[x][y]
,您正在寻址该特定单元格 。 In other words, "assign a value to cell [2][2] in the array." 换句话说,“为数组中的单元格[2] [2]赋值。”
So a[2][2] = 42;
所以
a[2][2] = 42;
is valid. 已验证。 But you can't assign an array to
a[2][2]
because it only holds one integer. 但是你不能将数组分配给
a[2][2]
因为它只包含一个整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.