Tremors
Tremors

Reputation: 133

Invalid Conversion from 'int' to 'int(*)[3]' c++

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

Upvotes: 2

Views: 1858

Answers (1)

MikeMB
MikeMB

Reputation: 21166

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.

Upvotes: 1

Related Questions