[英]size in multidimensional array in c++
this my code 这是我的代码
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int x,y, size;
int array[][2] = {{1,2}, {5,6}, {13, 16}, {17, 69}, {100, 200}};
for(x=0; x<5; x++){
for(y=0; y<2; y++){
cout<<array[x][y];
}
cout<<" ";
}
system("pause>nul");
return 0;
}
the code work fine. 代码工作正常。 But, when I replace line no.7
int array[][2]
to int array[][1]
, show error message like this : 但是,当我将第7行
int array[][2]
替换为int array[][1]
,显示如下错误消息:
64 E:\path\array_multi2.cpp:8 too many initializers for 'int [1]'
what the problem ? 有什么问题?
int array[][1]
declares an array with only one element in its second dimension. int array[][1]
声明一个在第二维中仅包含一个元素的数组。 You can't then initialize each of the elements in the first dimension with {1,2}
or {5,6}
, because that would require two elements. 然后,您无法使用
{1,2}
或{5,6}
初始化第一维中的每个元素,因为这将需要两个元素。
You would, for example, be able to initialize it like so: 例如,您将能够像这样初始化它:
int array[][1] = {{1}, {2}, {3}};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.