Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

passing argument 1 of 'strcmp' makes pointer from integer without a cast C

I'm trying to compare a 2D array with a char but it keeps sending me this error and pointing at the array.

if ((strcmp(matrixarray[j+k][i], "X"))==0 || (strcmp(matrixarray[j+k][i], currentword))==0)

Minimal reproducible example:

char wordsarray[8][7] = {"kalnas", "namas", "vanduo", "dama", "rasa", "ola", "mia", "jis"};
char matrixarray[6][7] = {"XOXXOX", "OOOOOO", "XOXXOX", "XOOOOX", "XOXOXX", "XOOOXX"};
char currentword = wordsarray[0][0];
int maxlen = 7 - sizeof(currentword);
        for (int i = 0; i < n; i++){
            for(int j = 0; j <= maxlen; j++){
                for (int k = 0; k < n; k++){
                    if ((strcmp(matrixarray[j+k][i], "X"))==0 || (strcmp(matrixarray[j+k][i], currentword))==0)
                        matrixarray[j+k][i] = currentword;
                    else
                        matrixarray[0][0] = 'X';
                }
question from:https://stackoverflow.com/questions/65840970/passing-argument-1-of-strcmp-makes-pointer-from-integer-without-a-cast-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Given the definition char matrixarray[6][7], calling strcmp(matrixarray[j+k][i], "X") passes a character to strcmp(), but it needs a pointer.

Using strcmp(&matrixarray[j+k][i], "X") is the simplest change. It will fix the compilation problem; it may or may not be correct algorithmically.

Indeed, it probably isn't correct. The comparison cannot succeed unless it is looking at the last character before the null byte in matrixarray[j+k]. It seems more likely that you should be comparing characters, not strings, so:

matrixarray[j+k][i] == 'X'

it probably more nearly correct.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...