warning: excess elements in array initializer

This is a discussion on warning: excess elements in array initializer within the C Programming forums, part of the General Programming Boards category; I'm not sure why this code is giving the error message "warning: excess elements in array initializer". I've taken a ...

  1. #1
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37

    warning: excess elements in array initializer

    I'm not sure why this code is giving the error message "warning: excess elements in array initializer". I've taken a look at:

    http://www.csc.liv.ac.uk/~grant/Teaching/COMP205/string.html

    and the example at the bottom seems to be similar to what I'm doing with this code. Can anyone shed any light on the matter. Any help is appreciated, thanks.



    Code:
    const int accepted_files = 13;
    /*compare user file types against file types accepted*/
    int
    file_type_compare(char *file_type)
    {
        char *accepted_file_types[accepted_files] = {
                "mp3",
                "wma",
                "wav",
                "m4a",
                "ogg",
                "pmg",
                "pls",
                "m3u",
                "bmp",
                "gif",
                "jpg",
                "png",
                "tiff"
        };
        int f;
        for(f = 0; f < accepted_files; ++f)    {
            if (!strcasecmp(*accepted_files[f], *file_type))    /*precaution against the user using mixed or upper case*/
                return 0;    /*exit, matched a type*/
        }
        die(_("File type \"%s\" matched no type, accepted file types are: put something here\n"), file_type);
    }
    The compile error:

    Code:
    metadata.c:1202: error: variable-sized object may not be initialized
    metadata.c:1203: warning: excess elements in array initializer
    metadata.c:1203: warning: (near initialization for ‘accepted_file_types’)
    metadata.c:1204: warning: excess elements in array initializer
    metadata.c:1204: warning: (near initialization for ‘accepted_file_types’)
    metadata.c:1205: warning: excess elements in array initializer
    metadata.c:1205: warning: (near initialization for ‘accepted_file_types’)
    metadata.c:1206: warning: excess elements in array initializer
    metadata.c:1206: warning: (near initialization for ‘accepted_file_types’)
    metadata.c:1207: warning: excess elements in array initializer
    metadata.c:1207: warning: (near initialization for ‘accepted_file_types’)
    metadata.c:1208: warning: excess elements in array initializer
    metadata.c:1208: warning: (near initialization for ‘accepted_file_types’)
    metadata.c:1209: warning: excess elements in array initializer
    metadata.c:1209: warning: (near initialization for ‘accepted_file_types’)
    metadata.c:1210: warning: excess elements in array initializer
    metadata.c:1210: warning: (near initialization for ‘accepted_file_types’)
    metadata.c:1211: warning: excess elements in array initializer
    metadata.c:1211: warning: (near initialization for ‘accepted_file_types’)
    metadata.c:1212: warning: excess elements in array initializer
    metadata.c:1212: warning: (near initialization for ‘accepted_file_types’)
    metadata.c:1213: warning: excess elements in array initializer
    metadata.c:1213: warning: (near initialization for ‘accepted_file_types’)
    metadata.c:1214: warning: excess elements in array initializer
    metadata.c:1214: warning: (near initialization for ‘accepted_file_types’)
    metadata.c:1216: warning: excess elements in array initializer
    metadata.c:1216: warning: (near initialization for ‘accepted_file_types’)
    metadata.c:1219: error: subscripted value is neither array nor pointer

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    The problem is that accepted_file_types is being declared as a variable length array. You probably need to change accepted_files to a macro or an enum.
    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
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hey what's this:
    Code:
          if (!strcasecmp(*accepted_files[f], *file_type))
    You are confused by your own variable names!

    Another problem about having near identical names is if you want to change one, you may be unable to do so using pattern matching without changing the others too.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37
    Oh dear, confusing variable names! Revisions have been made. And the warning messages have disappeared. I'm not sure how placing a const int or a macro would create create such a difference when specifying a char array size. If you could explain it further i would really appreciate it . I did originally start using a macro but in Steve Qualline's book "Practical C", he really pushes that the reader should use const over define whenever possible because of immediate syntax checking and scope rules etc. But I'm not sure what to think now.

    Code:
    #define FILE_COUNT 13
    /*compare user file types against file types accepted*/
    int
    file_type_compare(char *file_type)
    {
    	char *accepted_file_types[FILE_COUNT] = {
    			"mp3",
    			"wma",
    			"wav",
    			"m4a",
    			"ogg",
    			"pmg",
    			"pls",
    			"m3u",
    			"bmp",
    			"gif",
    			"jpg",
    			"png",
    			"tiff"
    	};
    	int f;
    	for(f = 0; f < FILE_COUNT; ++f)	{
    		if (!strcasecmp(*accepted_file_types[f], *file_type))	/*precaution against the user using mixed or upper case*/
    			return 0;	/*exit, matched a type*/
    	}
    	die(_("File type \"%s\" matched no type, accepted file types are: put something here\n"), file_type);
    }

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Shouldn't this:
    Code:
    		if (!strcasecmp(*accepted_file_types[f], *file_type))	/*precaution against the user using mixed or upper case*/
    be this
    Code:
    		if (!strcasecmp(*accepted_file_types[f], file_type))	/*precaution against the user using mixed or upper case*/

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Or you could leave out the array size such as the following...
    char *accepted_file_types[] = {
    The fact that you have initializers tells the compiler the required size.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    char *accepted_file_types[FILE_COUNT] = {
    Should really be:
    Code:
    const char *accepted_file_types[FILE_COUNT] = {
    although it's not required.




    Code:
    if (!strcasecmp(*accepted_file_types[f], *file_type))
    Should be:
    Code:
    if (!strcasecmp(accepted_file_types[f], file_type))
    ...both the *'s should be gone there.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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