简体   繁体   中英

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 :

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. 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.

You would, for example, be able to initialize it like so:

int array[][1] = {{1}, {2}, {3}};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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