2

I'm getting [Error] invalid conversion from 'int' to 'int(*)[3]' [-fpermissive] in a few spots of my code. This snippet in particular has that error

void getSquare(int square[3][3]){

    int column;

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cout << "Please enter a number between 1 and 9" << endl;
            cin >> column;
            cout << endl;
            square[i][j] = square[i][column];
        }
    }
}

This code is designed to take in 9 numbers and store them in a [3][3] array, I might have this completely wrong but let me know!

Here is how the code is being called for those of you who asked

int main(){
    int magicSquare[3][3];
    getSquare(magicSquare[3][3]);
    checkSquare(magicSquare[3][3]);
if (checkSquare(magicSquare[3][3]) == true)
    {
    cout << "Yes!"
    }
if (checkSquare(magicSquare[3][3]) != true)
    {
    cout << "No!"
    return 0;
    }
Tremors
  • 133
  • 1
  • 13
  • You probably want `square[i][j] = column;` instead of UB, else error is in calling code. – Jarod42 Jan 13 '16 at 23:15
  • `square[i][j] = square[i][column];` doesn't look correct, though I'm not sure what you are attempting to do with this piece of code. From a syntax perspective, how are you calling this function? Sounds like the compiler sees that a parameter for the function is an int[3][3] and you are attempting to give it a plain integer. – RyanP Jan 13 '16 at 23:15
  • What happens if the number the user enters for column is higher than 2? Your array is a 3x3. The indexes range from 0-2. So if column is 3+ it would go out of range. – Tarik Neaj Jan 13 '16 at 23:16
  • 1
    Your code doesn't make much sense, but I actually don't see how it would produce the error you claim. Can you provide a [mcve](/help/mcve) – MikeMB Jan 13 '16 at 23:19
  • @DietmarKühl He technically doesn't have to pass the array by reference. If the array is not passed by reference it will decay to a pointer and the changes he makes in the function will still be maintained after returning. He just won't be able to do an accurate `sizeof()` on the array. – RyanP Jan 13 '16 at 23:19
  • Are you going to give us a hint where in that snippet the error occurs, or do you expect people to guess? – Pete Becker Jan 13 '16 at 23:21
  • You have to include the call of the getSquare function, that's problably the source of the error. – Ilya Jan 13 '16 at 23:23
  • The posted code has no such error. See http://ideone.com/GCSyyR. – R Sahu Jan 13 '16 at 23:23

1 Answers1

1
int main(){ int magicSquare[3][3]; getSquare(magicSquare[3][3]); return 0; } 

Doesn't pass an array to the function, but the (non-exising) element in the 4th colum and 4th row (arrays are 0-indexed in c and c++). That is the reason for the error message, as you are trying to assign an integer (the matrix element) to a pointer to a three element array (that is what getSquare(int square[3][3]) actually expects - int square[3][3] is equivalent to int square(*)[3] in this context).

To pass the matrix you can write

int main(){ int magicSquare[3][3]; getSquare(magicSquare);} 

However, your getSquare will probably not do what you expect (it assigns one entry of the square to another one). You probably wanted to write

void getSquare(int square[3][3]) {

    int number;

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cout << "Please enter a number between 1 and 9" << endl;
            cin >> number;
            cout << endl;
            square[i][j] = number;
        }
    }
}

instead.

MikeMB
  • 20,029
  • 9
  • 57
  • 102