-1

I'm trying to write a sudoku program that verifies a completed board. Here is my code so far:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <pthread.h>

using namespace std;

int *board[9];
int row, col;
void is_row_ok(int* board[9][9]);
void is_col_ok(int* board[9][9]);
void is_square_ok(int* board[9][9]);

int main()
{
    for (int i = 0; i < 9; ++i)
    {
        board[i] = new int[9];
    }

    string line;
    ifstream myFile("Testfile1.txt");

    for (int row = 0; row < 9; ++row)
    {
        string line;
        getline(myFile, line);

        stringstream iss(line);
        cout << endl;

        for (int col = 0; col < 9; ++col)
        {
            string val;
            getline(iss, val, ',');
            if (!iss.good())
                break;

            stringstream convertor(val);
            convertor >> board[row][col];
            cout << board[row][col] << "  ";
        }
    }
    is_row_ok(&board[9][9]); //<-- error happens here
    pthread_create(thread1, NULL, is_row_ok, board[9][9]);
    //pthread_create(thread1, NULL, is_col_ok, board[9][9]);
    //pthread_create(thread1, NULL, is_square_ok, board[9][9]);

    cout << endl;
    return 0;
    }

void is_row_ok(int board[9][9])
{
    int element_count = 0;
    char element_value;
    for (int i = 0; i < 9; ++i)
    {
        for (int j = 0; j < 9; ++j)
        {
            element_count = 0;
            element_value = board[i][j];
            if (element_value != ' ')
            {
                for (int k = 0; k < 9; ++k)
                {
                    if (board[i][k] == element_value)
                        element_count++;
                }
            }
            if (element_count >= 2)
            {
                cout << "Row " << i << " is invalid." << endl;
            }
            else
            {
                cout << "Row " << i << " is valid." << endl;
            }
        }
    }
    //pthread_exit(NULL);
}

I'm getting the following error:

attempt.cpp:45:24: error: cannot convert ‘int*’ to ‘int* (*)[9]’ for argument ‘1’ to ‘void is_row_ok(int* (*)[9])’
  is_row_ok(&board[9][9]);

I'm not sure what's going on. I've looked at a bunch of similar situations on here and on other websites and I've tried to implement their solutions but none have worked. If anyone could please help me figure out what I need to change that would be greatly appreciated.

P.S. the functions is_col_ok() and is_square_ok() are very similar to the is_row_ok() function which is why they are not included.

P.P.S. if you could also take a look at my pthread creation function and tell me if I have done it correctly and if not what I need to change

P.P.S. if you need any additional code please don't hesitate to ask for it

Marc Karam
  • 393
  • 1
  • 16
  • The error happens because: `board[9][9]` resolves to a value of type `int`. So you take the address of that and wind up with a pointer to `int`, aka an `int*`. That does not match the type of value for the parameter which is expected by the function you are trying to call, hence the compiler error. – user268396 Mar 09 '17 at 21:29
  • `is_row_ok(&board[9][9]);` - what did you try to say by that? – AnT Mar 09 '17 at 22:23
  • http://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function – stark Mar 09 '17 at 22:46

1 Answers1

1

You mix up a 2D-array of ints, i.e. int board[9][9] as the type of your board-parameter in is_row_ok with an 1D-array of pointers to 1D-arrays of int, i.e. int *board[9] as the type of your board.

It's OK to define your board as a 1D-array of pointers to int[9] and initialise it as you did:

int *board[9];
int main() {
    for (int i = 0; i < 9; ++i)  {
        board[i] = new int[9];
    }
    ...

But then you have to declare + define your is_row_ok-function as

void is_row_ok(int* board[9]);
...
void is_row_ok(int* board[9]) { ...

and call it with

is_row_ok(board);

Note that in your original code declaration void is_row_ok(int* board[9][9]); does not match the definition of void is_row_ok(int board[9][9]) { ..., such that you have two (overloaded) functions is_row_ok, one that is declared before main but never defined, and another one with a different parameter type that is defined after main.

Stephan Lechner
  • 33,675
  • 4
  • 27
  • 49