Printing out Array Error

This is a discussion on Printing out Array Error within the C Programming forums, part of the General Programming Boards category; Hey guys im back with another question... The following code is theoriecly supposed to take whats inside the array, and ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Boston
    Posts
    25

    Printing out Array Error

    Hey guys im back with another question...

    The following code is theoriecly supposed to take whats inside the array, and shift it to the left... I'm having problems compiling around the array, getting an error saying:

    "variable-sized object may not be initialized"

    Any ideas why?

    heres my code:

    Code:
    	int  count;
    	int  size = 26;
    	char message_start[size] = {'a','ab','abc','abcd','abcde','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    	char message_end;
    	int pointer;
    	int head;
    	int tail;
    	char character;
    	count = 0;
    
    	while (count <= 19) {		// loop for shifting
    		wait_msec(DELAY_5MSEC); //Display Clear Command
    		write_lcd(REG, WR_DISPLAY_CMD1);	// Set Display On, Cursor On, Blink Cursor
    		wait_msec(DELAY_5MSEC);			// Command wait time
    		write_lcd(REG, WR_DISPLAY_CMD2);	// Clear Display
    		wait_msec(DELAY_5MSEC);	
    		
    		while (1)  
    			{      
    			write_lcd(REG, 0x93);
    			wait_msec(DELAY_5MSEC);	
    			write_lcd(REG, CURS_OFF);
    			wait_msec(DELAY_5MSEC);	
    				
    				for ( pointer == 0; pointer <= size; pointer++ )
    			{
    					lcd_puts(message_start[pointer]);}
    			
    			}

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Code:
                    int  size = 26;
    	char message_start[size] = {'a','ab','abc','abcd','abcde','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    You can't do that. Either you make the array size variable (only supported from C99, I think), in which case you can't use an initialiser list -> The compiler things that 'size' could be changed before you initialise message_start.
    In C89, you can't use a variable size array at all. You'd need to #define a constant to do it (which is a better idea anyway, unless you *really* need a variable size array)

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    "variable-sized object may not be initialized"

    Any ideas why?
    The variable size is not constant, so message_start is taken to be a variable length array (which are an extension to C, not standard).

    You may be able to do without size, e.g.,
    Code:
    char message_start[] = {'a','ab','abc','abcd','abcde','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    And then, assuming message_start has not decayed into a pointer:
    Code:
    for ( pointer = 0; pointer <= sizeof(message_start); pointer++ )
    {
        lcd_puts(message_start[pointer]);
    }
    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

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The C-standard doesn't allow that, yes.

    Since you just use a constant, 26, you should be able to change that so that it uses a constant size of your array, rather than using an initialized variable. If you are realy "lucky", you can possibly even stick "const" in front of the int size = 26 line, and the compiler will realize that the variable is a constant size.

    The other problem is that you have is that you are using more than one char in your array, eg:
    'a','ab','abc','abcd','abcde' - this (sort of) valid, but most likely not at all what you wanted anyways, as you probably would like to have a character string, not a multibyte single character. Use char *message_start[] and replace your single-quotes with double quotes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Location
    Boston
    Posts
    25
    Quote Originally Posted by matsp View Post
    The C-standard doesn't allow that, yes.

    Since you just use a constant, 26, you should be able to change that so that it uses a constant size of your array, rather than using an initialized variable. If you are realy "lucky", you can possibly even stick "const" in front of the int size = 26 line, and the compiler will realize that the variable is a constant size.

    The other problem is that you have is that you are using more than one char in your array, eg:
    'a','ab','abc','abcd','abcde' - this (sort of) valid, but most likely not at all what you wanted anyways, as you probably would like to have a character string, not a multibyte single character. Use char *message_start[] and replace your single-quotes with double quotes.

    --
    Mats
    Thank you both.

    Yeh turns out i am unlucky using the const... but i did manage to finally get it to compile using both your methods...

    my code now looks like this, but no output yet... gotta debug.

    Code:
    int  count;
    	//const int  size = 26;
    	char message_start[] = {"abcd"}; //'a','ab','abc','abcd','abcde','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    	//char message_start = "Message    ";
    	char message_end;
    	int pointer;
    	int head;
    	int tail;
    	char character;
    	count = 0;
    
    	while (count <= 19) {		// loop for shifting
    		//Display Clear Command
    		wait_msec(DELAY_5MSEC);
    		// Display On/Off Command
    		write_lcd(REG, WR_DISPLAY_CMD1);	// Set Display On, Cursor On, Blink Cursor
    		// Command wait time
    		wait_msec(DELAY_5MSEC);	
    		// Display Clear Command
    		write_lcd(REG, WR_DISPLAY_CMD2);	// Clear Display
    		// Command wait time
    		wait_msec(DELAY_5MSEC);	
    		
    		while (1) 
    			{     
    			write_lcd(REG, 0x93); // Starts Display at positing 0x13 or 20
    			// Command wait time
    			wait_msec(DELAY_5MSEC);	
    			// turns cursor off
    			write_lcd(REG, CURS_OFF);
    			// Command wait time
    			wait_msec(DELAY_5MSEC);	
    				
    				for ( pointer == 0; pointer <= sizeof(message_start); pointer++ )
    			{
    					lcd_puts(message_start[pointer]);}
    			
    			}
    
    
    		// Display On/Off Command
    		write_lcd(REG, WR_DISPLAY_CMD1);	// Set Display On, Cursor On, Blink Cursor
    		// Command wait time
    		wait_msec(DELAY_5MSEC);

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    for ( pointer == 0; pointer <= sizeof(message_start); pointer++ )
    Perhaps you meant pointer = 0? Plus "pointer" is a somewhat misleading name for that variable.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM

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