简体   繁体   English

正确使用strcmp功能?

[英]Proper use of the strcmp function?

Can someone please explain to me how to properly use the strcmp function? 有人可以向我解释如何正确使用strcmp功能吗? I'm creating a tic-tac-toe game and I keep getting the error: 我正在创建一个tic-tac-toe游戏,我不断收到错误:

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. 我创建了两个指针作为strcmp函数的参数。 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;
}

As others have already pointed out, the second argument should be of type char* and not char . 正如其他人已经指出的那样,第二个参数应该是char*类型而不是char

I just wanted to mention that the series of if statements can be rewritten as a for loop: 我只想提一下if语句系列可以重写为for循环:

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. 每次调用函数时重新初始化moves是否有意义,以及str的无效值是否应该引发错误也是值得考虑的。

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). 请注意从char (单个字符)到char * (字符串)的更改。

Also make sure you have included string.h at the top of your file: 还要确保在文件顶部包含string.h

#include <string.h>

In the function arguments, you have declared "str" as "char". 在函数参数中,您已将“str”声明为“char”。 It should be "char*". 它应该是“char *”。

strcmp需要一个指向字符数组的指针,但str应该是char*时声明为单个字符。

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 strcmp()在字符串匹配时返回0,因此在控制语句中写入时更喜欢写入

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 在第一个等式中,if语句执行strcmp返回给它的NOT,所以如果字符串相等,你将获得0而NOT 0是1

So it works saving tons of your time typing. 所以它可以节省大量的时间打字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM