Warning: excess elements in scalar initializer

This is a discussion on Warning: excess elements in scalar initializer within the C Programming forums, part of the General Programming Boards category; Hi everyone! I get this warning warning: (near initialization for `Fingers') warning: excess elements in scalar initializer when compiling: Code: ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    Warning: excess elements in scalar initializer

    Hi everyone!

    I get this warning
    warning: (near initialization for `Fingers')
    warning: excess elements in scalar initializer
    when compiling:

    Code:
    int Fingers[6] = {0,0,0,0,0,0};
    I've searched the web but the problems related with this warning are mostly due to pointers usage.
    Since I'm initializing the variable with 6 int's, I don't get what is happening.

    Thanks for your responses!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Here's a simple test: compile this program:
    Code:
    int main(void)
    {
        int Fingers[6] = {0,0,0,0,0,0};
        return 0;
    }
    Do you get the same warning?

    You might also consider writing:
    Code:
    int Fingers[] = {0,0,0,0,0,0};
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    979
    The warning message makes it sound like your compiler (which I presume is gcc from the diagnostic) thinks you're initializing something other than an array—it would tell you “excess elements in array initializer” otherwise.

    Does this happen with the simplest of programs (i.e., just that line and nothing else)? If so, it's surely a bug, either in the compiler itself or in its installation. If this does not happen, your code has a bug elsewhere that's causing the problem. You'll want to paste the whole code, if it's reasonably short.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Hi laserlight and thanks for your answer!

    With your test code from above, I don't get the warning! (but I don't understand why, I mean, the declaration isn't just the same? :S)

    With the declaration
    Code:
    int Fingers[] = {0,0,0,0,0,0};
    the warning still persists.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Okay, then you need to start posting more code. It would be good if you could post the smallest and simplest program that demonstrates the warning.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Hi cas!

    Here is the whole code:
    Code:
    int staticGestureTreatment(int SeqFingers[], int FingerCountFramesNum)
    {
        int i;
        float prob = 0;
        int option = -1;
        int Fingers[6] = {0,0,0,0,0,0};
        int totFingers;
        int max_value = -1;
        
        totFingers = FingerCountFramesNum;
        
        for(i=0; i<FingerCountFramesNum; i++)
        {
            switch(SeqFingers[i])
            {
                case 0:
                    Fingers[0]++;
                    break;
                case 1:
                    Fingers[1]++;
                    break;
                case 2:
                    Fingers[2]++;
                    break;
                case 3:
                    Fingers[3]++;
                    break;
                case 4:
                    Fingers[4]++;
                    break;
                case 5:
                    Fingers[5]++;
                    break;
                default:
                    totFingers--;
                    printf("Incorrect hand position, Fingers: %d\n", SeqFingers[i]);        
            }
        }
        
        max_value = 0;
        for (i=0; i<6; i++)
        {
            if(Fingers[i] > max_value)
            {
                max_value = Fingers[i];
                option = i;
            }
        }
    
        prob = (float) max_value/totFingers;
        return(option);
    }
    From my previous reply, the warning isn't there with the simpliest test code.

    Thank you!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by TeRMaL
    Here is the whole code:
    And this is the function, as-is, that results in the warning that you mentioned? What is the exact version of your compiler?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Quote Originally Posted by laserlight View Post
    And this is the function, as-is, that results in the warning that you mentioned? What is the exact version of your compiler?
    Well, the function is included on a much bigger code but this function as I posted it gives me that warning.

    I'm not sure how to get neither the compiler nor the version :S. I'm using an IDE called NIOS II (running over eclipse); I've been searching through options but couldn't find anything.
    I think it doesn't use gcc since when I type gcc in DOS the command is not recognized.

    I've been trying some configurations and the code
    Code:
    int main(void)
    {
        int i;
        float prob = 0;
        int option = -1;
        int Fingers[6] = {0,0,0,0,0,0};
        int totFingers;
        int max_value = -1;
        
        for(i=0; i<9; i++)
        {
            switch(option)
            {
                case 0:
                    Fingers[0]++;
                    break;
                case 1:
                    Fingers[1]++;
                    break;
                case 2:
                    Fingers[2]++;
                    break;
                case 3:
                    Fingers[3]++;
                    break;
                case 4:
                    Fingers[4]++;
                    break;
                case 5:
                    Fingers[5]++;
                    break;
                default:
                    totFingers--;
            }
        }
        
        max_value = 0;
        for (i=0; i<6; i++)
        {
            if(Fingers[i] > max_value)
            {
                max_value = Fingers[i];
                option = i;
            }
        }
        prob = (float) max_value/totFingers;
        
        return(option);
    }
    does not give the warning. It's same code but without the input variables for the function declaration :S (and some nonsense changes not to get an error ).

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    Quote Originally Posted by TeRMaL View Post
    Well, the function is included on a much bigger code but this function as I posted it gives me that warning..
    Then look in the parts of the "much bigger code" that you have elected not to show us.

    The most likely explanation I can think of is that the "much bigger code" includes some macro definitions that are changing the meaning of your code. For example, there is an #include directive or a #define directive (or a header file that #define's some macro) in play.

    If you are using a gcc command line option that defines a macro (eg -D<macro=something>) that would also explain the error message.
    Right 98% of the time, and don't care about the other 3%.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Quote Originally Posted by grumpy View Post
    Then look in the parts of the "much bigger code" that you have elected not to show us.

    The most likely explanation I can think of is that the "much bigger code" includes some macro definitions that are changing the meaning of your code. For example, there is an #include directive or a #define directive (or a header file that #define's some macro) in play.

    If you are using a gcc command line option that defines a macro (eg -D<macro=something>) that would also explain the error message.
    Hey grumpy!
    The reason why I haven't posted the rest of the code is because it will be unworkable (it has hundreds of code lines)!

    I'm not using any macro so this couldn't be the problem :S.
    It seems unlikely to me that a #define directive could cause problems with an array declaration but I will re-check on this! (I guess I'm searching for #define's which declare a similar variable which with I'm having trouble?)

    About using macro definitions in the command line, that's not the case.

    Greetings, TeRMaL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning: excess elements in array initializer
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 09-30-2009, 06:08 AM
  2. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21