简体   繁体   中英

Compilation Warning.Excess elements in array initializer

I was implementing a multi dimensional array and using pointers and testing the correctness of the address allotment.Even though the program ran perfectly and all the addresses were same as i expected.But there was a compilation warning [Warning] excess elements in array initializer .Can anyone explain about the warning.The code is below....

#include<stdio.h>
    int main(){
    int c[3][2][2] = {{{2,5},{7,9},{3,4},{6,1},{0,8},{11,13}}};
    printf("%d %d %d %d",c,*c,c[0],&c[0][0]);
    return 0;
}

The error summary is like this

        In function 'main':
3   2   [Warning] excess elements in array initializer
3   2   [Warning] (near initialization for 'c[0]')
3   2   [Warning] excess elements in array initializer
3   2   [Warning] (near initialization for 'c[0]')
3   2   [Warning] excess elements in array initializer
3   2   [Warning] (near initialization for 'c[0]')
3   2   [Warning] excess elements in array initializer
3   2   [Warning] (near initialization for 'c[0]')

You have three pairs of a pair of int s. The initialization should be:

int c[3][2][2] = {{{2,5},{7,9}},{{3,4},{6,1}},{{0,8},{11,13}}};
      3                 ^             ^             ^
         2           ^     ^
            2       ^ ^          

That is not a three dimensional array. You forgot a brace!

int c[3][2][2] = {{{2,5},{7,9}},{{3,4},{6,1}},{{0,8},{11,13}}};

Perhaps reformat things to make it clearer:

int c[3][2][2] = {
    { {2,5}, {7,9} }, 
    { {3,4}, {6,1} },
    { {0,8}, {11,13} }
};

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