Thread: excess elements in array initializer

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    66

    excess elements in array initializer

    Can anyone tell what warning the compiler is giving and how to overcome it?

    Code:
     warning: excess elements in array initializer
    { {10, 20, 30, 40}, {50, 60, 70, 80}, {90, 100, 110, 120}},
    ^
    note: (near initialization for 'array')
    Code:
    #include<stdio.h>          
    int main ()
    {  
       
       int arr[3][3][3]=   
            {
                { {11, 12, 13}, {14, 15, 16}, {17, 18, 19} },
                { {21, 22, 23}, {24, 25, 26}, {27, 28, 29}},
                { {31, 32, 33}, {34, 35, 36},{37, 38, 39} },
            };
            
       int array [2][3][4] = 
    
    
       {
           { {1, 2, 3}, { 4, 5, 6} },
           { {11, 12, 13}, {14, 15, 16}, {17, 18, 19}},
           { {10, 20, 30, 40}, {50, 60, 70, 80}, {90, 100, 110, 120}},
       };
       
      return 0;     
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,001
    "excess elements in array initializer" sounds pretty straightforward: you're trying to initialise the array with too many elements. For example, in this case you declared the array as an int[2][3][4], but you're trying to initialise it as if it were an int[3][3][4].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    66
    Quote Originally Posted by laserlight View Post
    "excess elements in array initializer" sounds pretty straightforward: you're trying to initialise the array with too many elements. For example, in this case you declared the array as an int[2][3][4], but you're trying to initialise it as if it were an int[3][3][4].
    so int array [2][3][4] -> 2*3*4 = 24 elements

    Code:
       { {1, 2, 3}, { 4, 5, 6} },  // 6 elements        { {11, 12, 13}, {14, 15, 16}, {17, 18, 19}},// 9 elements 
           { {10, 20, 30, 40}, {50, 60, 70, 80}, {90, 100, 110, 120}}, //12 elemnets
    I do not understand where the size is increasing

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    740
    The array has only 2 rows. How many rows are you trying to fit in it? I counted 3:

    Code:
       int array [2][3][4] = 
    
    
    
       {
           { {1, 2, 3}, { 4, 5, 6} },                                  // <== 1
           { {11, 12, 13}, {14, 15, 16}, {17, 18, 19}},                // <== 2
           { {10, 20, 30, 40}, {50, 60, 70, 80}, {90, 100, 110, 120}}, // <== 3 (too many!)
       };
    The compiler is rightly telling you that you cannot do that.

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    66
    Quote Originally Posted by christop View Post
    The array has only 2 rows. How many rows are you trying to fit in it? I counted 3:
    .
    I am confuse because there are so many way I do not understand how you counted row.

    Code:
    #include<stdio.h> int main() 
    { 
    
    
      int array [2][3][4] =  
     
    {
        { {11, 12, 13, 14}, {15, 16, 17, 18}, {17, 18, 19, 20}},        
        { {10, 20, 30, 40}, {50, 60, 70, 80}, {90, 100, 110, 120}},
    };
    
    
        return 0; 
    }

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    740
    C'mon, are you just trolling us now? I numbered the rows for you.

    Your new code is correct, by the way.

  7. #7
    Registered User
    Join Date
    Nov 2019
    Posts
    66
    Quote Originally Posted by christop View Post
    C'mon, are you just trolling us now? I numbered the rows for you.

    Your new code is correct, by the way.
    sorry now I understood what I was doing wrong

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ouput .bpm excess elements in scalar initializer
    By Martta in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2014, 11:29 AM
  2. warning: excess elements in struct initializer
    By sergioms in forum C Programming
    Replies: 5
    Last Post: 12-06-2011, 11:25 AM
  3. Warning: excess elements in scalar initializer
    By TeRMaL in forum C Programming
    Replies: 9
    Last Post: 10-08-2010, 03:54 AM
  4. 3D array initializer list
    By kenryuakuma in forum C# Programming
    Replies: 5
    Last Post: 11-15-2009, 01:40 AM
  5. warning: excess elements in array initializer
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 09-30-2009, 06:08 AM

Tags for this Thread