[英]Excess elements in array initializer
I'm currently trying to print a table, but I keep getting an error that I have too many elements in my array initializer. 我当前正在尝试打印表,但是我不断收到一个错误,表明我的数组初始化器中包含太多元素。 What did I do wrong?
我做错了什么? (Using Xcode)
(使用Xcode)
int coordinates[5] [2] = {{x1,x2,x3,x4,x5},{y1,y2,y3,y4,y5}};
int coordinates[5] [2]
is an array of five arrays of two int
elements each. 是一个由五个数组组成的数组 ,每个数组包含两个
int
元素。
Judging by your initialization, you want coordinates
to be an array of two arrays of five int
elements each: 通过初始化判断,您希望
coordinates
是两个数组,每个数组包含五个int
元素:
int coordinates[2] [5]
Such an initializer used in an array declaration 数组声明中使用的此类初始化程序
{x1,x2,x3,x4,x5}
corresponds to a one-dimensional array that has at least 5 elements. 对应于具有至少5个元素的一维数组。 However this declaration
但是这个声明
int coordinates[5] [2] = { /*...*/ };
corresponds to an array elements of which are one-dimensional arrays of the type int[2]
. 对应于一个数组元素,该数组元素是
int[2]
类型的一维数组。 So you may use an initializer list at most with two initializers to initialize each element of the two-dimensional array. 因此,您最多可以使用带有两个初始化程序的初始化程序列表来初始化二维数组的每个元素。
It seems that what you need is the following 似乎您需要的是以下内容
int coordinates[5] [2] =
{
{ x1, y1 },
{ x2, y2 },
{ x3, y3 },
{ x4, y4 },
{ x5, y5 }
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.