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

Parents
  • 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

Reply
  • 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

Children