Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...