2

Here is the code I have. I'm trying to do a string comparison. A serial input reads what keys are pressed and sets cmd.command to what was typed on the keyboard. Then I take that and do a string comparison to see if it is a command that's within my list. What I'm stuck on is the string comparison.

typedef struct {
    const char *cmd;
    void (*cmdFuncPtr)(void);
}CmdStruct;

typedef struct {
    char command[16];
    char argument[16];
} Command;

Command cmd;

CmdStruct cmdStructArray[] = { {"led",      LEDHandler      },
                               {"relay",    RelayFunction    },  };

void ProcessCommand() {
    for (j = 0; j < sizeof(cmdStructArray)/sizeof(cmdStructArray[0]); j++) {
        if(strcmp(cmdStructArray[j].cmd, cmd.command) == 0) {
            // do stuff
        }
    }
}

If I type in "led", then these two printf statements print the same thing.

printf(cmdStructArray[0].cmd);
printf("%s", cmd.command);

How can I get the string comparison to work?

Jack
  • 5,264
  • 7
  • 34
  • 43
  • 3
    How do you get your input? Probably `fgets`, then remove the trailing newline. – Daniel Fischer Jan 10 '13 at 00:35
  • Yeah, there will be some subtle difference between the strings. Perhaps a trailing newline, perhaps a difference in case. – David Schwartz Jan 10 '13 at 00:36
  • `strcmp()` works just fine. What problem are you having? And where did `cmd` come from? I don't see where that's declared. – Jonathan Wood Jan 10 '13 at 00:37
  • 3
    Use `printf("'%s'\n", XXX);` and it will be obvious. – David Schwartz Jan 10 '13 at 00:37
  • You should also make sure both strings are null terminate. When you initialize the struct these strings are null terminated. You can add '\0' as the last character of the array you type in. – Grieverheart Jan 10 '13 at 00:39
  • I read text in from a serial port, and as each letter is typed, it is set to cmd.command array. A counter is incremented of course to fill up the array correctly. I am sure there is no newline character that is put into cmd.command. – Jack Jan 10 '13 at 00:42
  • And yes, the cmd.command also is null terminated after person presses enter on the keyboard. – Jack Jan 10 '13 at 00:43
  • @Jack What is the value of `strlen(cmd.command)`? – ouah Jan 10 '13 at 00:44
  • How do you know the string comparison is not working? Did you try to add an `else` block and print both strings inside of it? Then, if you print them as suggested by @DavidSchwartz, you'll probably figure it out. – igorrs Jan 10 '13 at 03:11
  • @ouah I added printf("%d",strlen(cmd.command)); and printf("%d",strlen(cmdStructArray[0].cmd)); The result when I typed in "led" was 3 and 1001. – Jack Jan 10 '13 at 17:33
  • @ igorrs I know the strcmp wasn't working when I did an if/else block and printed both. The output was crazy for printing out cmd.command. I did printf(cmd.command); I trust that strcmp itself is fine, but what I'm putting in is not matching. – Jack Jan 10 '13 at 17:33
  • Could you declare `cmd` as `const char * const cmd` in the struct and then recompile the code? This is just to make sure `cmd` is not being changed after it's initialized. Also... what compiler are you using? – igorrs Jan 11 '13 at 23:22

2 Answers2

0

Your cmd.command commands likely have hidden trailing whitespace. Strip the whitespace before running comparisons. (Thanks David Schwartz in the comments!)

StilesCrisis
  • 15,972
  • 4
  • 39
  • 62
0

I found a fix, and now strcmp works. I changed the struct in the struct array. Now it's

typedef struct {
    char cmd[16];
    void (*cmdFuncPtr)(void);
}CmdStruct;

I don't know why this works, and don't know what the difference is. The const char *cmd I had before is also a way to create a "string" in C.

Jack
  • 5,264
  • 7
  • 34
  • 43
  • mmm not quite. this way, storage space is actually allocated for your string buffer, whereas the other way, you were just pointing to a memory location. Anything messing with that location would kill your "string" the old way – im so confused Jan 10 '13 at 17:56
  • Your original code looks correct too: the `cmd` fields were being initialized as pointers to memory with static storage duration (string literals). – igorrs Jan 11 '13 at 23:20