1

Can someone please explain to me how to properly use the strcmp function? I'm creating a tic-tac-toe game and I keep getting the error:

passing argument 1 of ‘strcmp’ makes pointer from integer without a cast

I've created two pointers that act as parameters for the strcmp function. One is the input that the player puts in, the second is the selection of moves the player has. However, when I try to run the code I get the error above. Below is a piece of my code:

void mark_location(int userU, char str) {
    char *moves[] = {"upperLeft", "up", "upperRight", "left", "center", "right", "lowerLeft", "down", "lowerRight"};

    if (strcmp(str, moves[0]) == 0)
        board[0][0] = userU;
    else if (strcmp(str, moves[1]) == 0)
        board[0][1] = userU;
    else if (strcmp(str, moves[2]) == 0)
        board[0][2] = userU;
    else if (strcmp(str, moves[3]) == 0)
        board[1][0] = userU;
    else if (strcmp(str, moves[4]) == 0)
        board[1][1] = userU;
    else if (strcmp(str, moves[5]) == 0)
        board[1][2] = userU;
    else if (strcmp(str, moves[6]) == 0)
        board[2][0] = userU;
    else if (strcmp(str, moves[7]) == 0)
        board[2][1] = userU;
    else if (strcmp(str, moves[8]) == 0)
        board [2][2] = userU;
}
Alex Riley
  • 169,130
  • 45
  • 262
  • 238
user1064913
  • 335
  • 2
  • 8
  • 19
  • 6
    `mark_location(int userU, char *str)` – wildplasser Jan 22 '12 at 19:06
  • The error has to do with your type. However, there are some problems with your choice of determining which move to pick (for example, what should happen if an invalid move is selected?). Do you think you could turn the strings into an enum and then run a switch statement? – Richard J. Ross III Jan 22 '12 at 19:10

5 Answers5

4

As others have already pointed out, the second argument should be of type char* and not char.

I just wanted to mention that the series of if statements can be rewritten as a for loop:

void mark_location(int userU, char* str) {
    char *moves[] = {"upperLeft", "up", "upperRight", "left", "center", "right", "lowerLeft", "down", "lowerRight"};
    int i;
    for (i = 0; i < 9; i++) {
        if (strcmp(str, moves[i]) == 0) {
            board[i / 3][i % 3] = userU;
            break;
        }
    }
}

It may also be worth considering whether it makes sense to re-initialize moves every time the function is called, and whether an invalid value of str should raise an error.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

Change your function declaration to the following:

void mark_location(int userU, char *str) {

Note the change from char (a single character) to char * (a string).

Also make sure you have included string.h at the top of your file:

#include <string.h>
1

In the function arguments, you have declared "str" as "char". It should be "char*".

Alex D
  • 29,755
  • 7
  • 80
  • 126
1

strcmp expects a pointer to an array of characters, but str is declared as a single character, when it should be char*.

Necrolis
  • 25,836
  • 3
  • 63
  • 101
0

Try doing this:

for (i = 0; i < 9; i++) {
    if (!strcmp(*str, *moves[i]) ) {
        board[i / 3][i % 3] = userU;
        break;
    }
}

One more thing for saving typing effort:

strcmp() returns 0 when strings match so while writing that in a control statement prefer writing

if(!strcmp(hello, world)){/* do this do that*/}.....1

instead of writing

if(strcmp(hello, world)==0){/* do this do that*/}......2

in the first equation the if statement does the NOT of whatever strcmp returns to it so if the strings are equal you'll get a 0 and NOT 0 is a 1

So it works saving tons of your time typing.

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
thewhitetulip
  • 3,235
  • 3
  • 21
  • 26