0

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]')

2 Answers 2

2

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

int c[3][2][2] = {{{2,5},{7,9}},{{3,4},{6,1}},{{0,8},{11,13}}};
      3                 ^             ^             ^
         2           ^     ^
            2       ^ ^          
Sign up to request clarification or add additional context in comments.

5 Comments

Worked !! But can you explain the warning.
@SaiKiranUppu The brackets were not correct for the specified dimensions. That is it.
Can you please explain i cant figure out what your answer is
That's not three pairs of two ints; a "pair of two ints" has only two ints. You were right in your original wording (edited out in the grace period).
@user2357112 I agree it was ambiguous, so I made it clearer.
1

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} }
};

2 Comments

Your new initializer is for a 2x3x2 array, not 3x2x2.
@user2357112 Thanks, it seems I can't count.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.