Search code examples
cstrcmp

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';
                }

Solution

  • 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.