简体   繁体   中英

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)

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.

Judging by your initialization, you want coordinates to be an array of two arrays of five int elements each:

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. However this declaration

int coordinates[5] [2] = { /*...*/ }; 

corresponds to an array elements of which are one-dimensional arrays of the type 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 } 
};

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