0

I am writing a program to solve Sudoku puzzles in C I declared the array in main() when I try and access it in a different function the compiler gives the error I've tried using pointers to the array but nothing seems to allow me to access it

error: ‘grid’ undeclared (first use in this function) 121 |
printf("%d ", &grid[i][i]);

How can I access this array from a different function?

Here is the code

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main()
{
    int grid[9][9] = 
    {
        {8,0,0,0,0,0,0,0,0},
        {0,0,3,6,0,0,0,0,0},
        {0,7,0,0,9,0,2,0,0},
        {0,5,0,0,0,7,0,0,0},
        {0,0,0,0,4,5,7,0,0},
        {0,0,0,1,0,0,0,3,0},
        {0,0,1,0,0,0,0,6,8},
        {0,0,8,5,0,0,0,1,0},
        {0,9,0,0,0,0,4,0,0}
    };

    int LineNum = 9;
    int RowAmount = 9;

    for (int i = 0; i < LineNum; ++i)
    {
        for (int i = 0; i < RowAmount; ++i)
        {
            printf("%d ", &grid[i][i]);
        }

        printf("\n");   
    }
}

int find_empty_box(int sudoku)
{
    for (int x = 0; x <= 9; ++x)
    {
        for (int y = 0; y < 9; ++y)
        {
            if (sudoku[&x][&y] == 0)
            {
                return x, y;
            }
        }
        return NULL, NULL;
    }
}

int Answer_Valid(int sudoku, int guess, int row, int col)
{
    int row_values = &sudoku[&row];

    for (int i = 0; i < sudoku[&row]; ++i)
    {
        if (guess == sudoku[&row[&i]])
        {
            return false;
        }
    }

    int column_values = &sudoku[&col];

    for (int t = 0; t <= 9; ++t)
    {
        for (int n = 0; n < &sudoku[&col]; ++n)
        {
            if (&guess == &sudoku[&col[&n]])
            {
                return false;
            }
        }
    }

    int row_start = (row / 3) * 3;
    int col_start = (col / 3) * 3;

    for (int x = 0; x <= row_start && row_start + 3; ++x)
    {
        for (int y = 0; y < col_start && col_start + 3; ++y)
        {
            if (sudoku[&x][&y] == guess)
            {
                return false;
            }
            return true;
        }
    }

}

int Solver(sudoku, guess)
{
    int row, col = find_empty_box(sudoku);

    if (row == NULL)
    {
        return true;
    }

    for (int i = 0; i < 1||2||3||4||5||6||7||8||9; ++i)
    {
        if(Answer_Valid(sudoku, guess, row, col))
        {
            sudoku[&row][&col] = guess;

            if(Solver(sudoku))
            {
                return true;
            }
            return false;
        }
    }

    int LineNum = 9;
    int RowAmount = 9;

    for (int i = 0; i < LineNum; ++i)
    {
        for (int i = 0; i < RowAmount; ++i)
        {
            printf("%d ", &grid[i][i]);
        }

        printf("\n");   
    }
}
Amir Charkhi
  • 768
  • 7
  • 23
  • 2
    There's no `grid` variable in the `Solver` function. Why do you expect that to work? Do you understand the concept of "scope"? The variable needs to be in a scope that the function can access - e.g. global or passed in as a variable. Also, the function is not well defined - missing parameter types. – kaylum Aug 11 '21 at 00:59
  • Does this answer your question? [How to pass 2D array (matrix) in a function in C?](https://stackoverflow.com/questions/3911400/how-to-pass-2d-array-matrix-in-a-function-in-c) – kaylum Aug 11 '21 at 01:04
  • If `Solver` needs to access `grid`, then pass it as an argument to `grid`. – Tom Karzes Aug 11 '21 at 01:05

0 Answers0