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

efficient varied string table

Please help. I have been struggling for too long on this. How do I use printf to print any of the messages?

Is this the best way to use a table of varying length strings or is a struct devicestates {}; better, or must I specify fixed length strings in the struct?

char DeviceStates[4][2][25] =
{
  {"closed", "open"},
  {"down", "up"},
  {"on;", "off"},
  {"say not implemented;", "say is implemented;"}
};

  • "Is this the best way..."

    Depends on what you mean by "best"

    It is certainly a very bad way if you are concerned to minimise the amount of storage used!

    The whole point of arrays is that every element is the same - in your case, the strings vary greatly in length, so the short ones will be padded to the length of the longest ones.
    This wastes a lot of space - which may or may not be important to you.

    You could avoid this by using pointers to the strings instead; eg,

    char *DeviceStates[4][2] =
    {
      {"closed", "open"},
      {"down", "up"},
      {"on;", "off"},
      {"say not implemented;", "say is implemented;"}
    };