Pass char[] to Serial.println

I know it's been asked and probably answered but I still don't get it...

Why can't I send the string to println?

I have:

typedef struct{
    char key;
    char command[];
} command;

command commands[] = {
  { 11, "LIGHTS_TEST_SW" },
  { 12, "MASTER_MODE_AA" }
};

// hooking the key pad up and more...

void keypadEvent(KeypadEvent KEY) {
  KeyState state = keypad.getState();
  char key = keypad.getKey();
  for (int i=0; i<sizeof(commands); i++) {
    if (commands[i].key == KEY) {
      // this fails...
      Serial.println(commands[i].command);
    }
  }
}

That's going to loop way beyond the two elements of your array.

i<sizeof(commands) / sizeof (commands[0])

Would be more normal.

I would choose different names for different things, maybe you confuse the compiler also.
How about char text[16] inside the command struct?

BTW you can not use the unspecified length for the char array like you do.

struct command {
  char key;
  char text[16];
};

Use const char *

struct Command {
    char key;
    const char *command;
};
 
Command commands[] = {
  { 11, "LIGHTS_TEST_SW" },
  { 12, "MASTER_MODE_AA" }
};

here is working example 7UONDK - Online C++ Compiler & Debugging Tool - Ideone.com

Ah, that explains why I got a lot of garbage when checking the values. Also added a break to quit the loop if found.

Thanks

Thanks, cleaned it up properly.

I was going to bed annoyed but now I can get a good nights sleep :slight_smile: