This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Declaring (initializing) array without size

Hi!

I'm having some problems declearing an array. I am trying to make an array that contains messages that I will later display on an LCD display. I am using a 2D char array, but I don't want to specify a fixed size to the second dimension. At the moment i am doing this:

const char message[][20] = {
	"RS232 Monitor",
	"By: Oyvind Tjervaag",
	"V1.0",
};

The problem with this is that a lot of space is wasted scince there is only one string that fills up the 20 chars. Here the "V1.0" string will be the 4 chars I want to use followed by 16 0x00...

The annoying thing about this is that I have have done it before.. But I can't find the code where I did it...

I thought I could do it like this:

const char message[][] = {...}
But that is just giving me the error, "Too many initializers" , which of course means that I am trying to add more variables than what can fit into the array.

Could someone give me any hints about this?

Thanks
Øyvind

  • You are creating a 2-dimensional array. What you probably want is an array of pointers (to strings). For example:

    char *array [] =
      {
      "This",
      "is",
      "an",
      "array",
      "of",
      "pointers",
      "to",
      "strings.",
      };
    

    array[0] points to "this"
    array[1] points to "is"
    and so on.

    Jon

  • Ahh... Yes, just tried it and that seems to be exactly what I need..

    Thanks Jon,

    Øyvind

  • Hi,

    Nice idea. I'm ew in embedded C and working on a nice project with a LCD. Therefore I'm writing a Hitachi 44780 module. It is workig perfect ut what I like to know is how to put a string to a LCD. The following a tried:

    /*
    //  Write a string to the LCD *** Doesn't work at the moment *** Help needed!!!
    */
    void LCD_String (char ROW, char COL, char *string)
    {
    	GoToXY (ROW, COL);
    	while (*string)
    		LCDdata (*string++);
    }
    

    char lcdstr = "LCD Ready";
    LCD_String(1,1,*lcdstr);
    

    If I try this, nothing apears on the LCD.

    "GoToXY" and "LCDdata" are working perfectly because I tried it seperatly.

    If I introduce the "LCD_String", No string on the LCD.

    Do I need to include some libraries?
    Or what else do I wrong?

  • You have defined lcdstr to be a single char, whereas you need a string:

    Try either

    char * lcdstr = "LCD Ready";
    Or,
    char lcdstr [] = "LCD Ready";
    and then lose the '*' in the function call

  • Thanks for the fast reply.

    But.... neither functions seems to work.
    The first one come through the compiler but no string on the LCD. Infact, same result as I allready had.

    The other seems to bother the compiler.
    I get an error message error: need array size

    So.... I got dont know what to do....

  • Is this what you ended up with?
    (This works fine for me.)

    void LCD_String (char ROW, char COL, char *string)
    {
    	GoToXY (ROW, COL);
    	while (*string)
    		LCDdata (*string++);
    }
    
    
    
    char lcdstr[] = "LCD Ready";
    LCD_String(1,1,lcdstr);