C: 警告 returning void * from a function with return type in makes integer from pointer without cast

C: warning returning void * from a function with return type in makes integer from pointer without cast

我正在编写一个程序来使用回溯算法解决数独谜题,当我编译我的代码时,编译器 returns 一条警告指出以下内容在函数 find_empty_box()

warning: returning ‘void *’ from a function with return type ‘int’ makes integer from pointer without a cast [-Wint-conversion]

我已经尝试将函数初始化为 void *(我也尝试将它初始化为 int 但这会导致关于返回 NULL 的警告,这表明我应该将函数声明为 void * 它确实解决了之前的警告但是创建了两个其他警告 第一个警告是在函数 find_empty_box() 中引起的 第二个警告是在 Solver()

中引起的
warning: returning ‘int’ from a function with return type ‘void *’ makes pointer from integer without a cast [-Wint-conversion]
   13 |                                 return x, y;

warning: assignment to ‘int’ from ‘void *’ makes integer from pointer without a cast [-Wint-conversion]
   78 |         row, col = find_empty_box(sudoku);

程序的代码是

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

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(int sudoku, int guess, int row, int col)
{
    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}
    };

    row, col = find_empty_box(sudoku);

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

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

            if(Solver(sudoku, guess, row, col))
            {
                return true;
            }
            return false;
        }
    }

    int LineNum = 9;
    int RowAmount = 9;

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

        printf("\n");   
    }
}

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 x = 0; x < RowAmount; ++x)
        {
            printf("%d ", &grid[i][x]);
        }

        printf("\n");   
    }
}

虽然错误是不言自明的(你在 void* 中将函数声明为 return 但你试图 return 一对整数),底层问题是 C 不支持像这样的 'pairs' 或 'tuples',所以你最好使用额外的指针参数来 return 这些值,并且只使用 return succeed/fail 布尔标志的值。所以你的 find_empty_box 函数变成:

bool find_empty_box(int sudoku[9][9], int *row, int *col)
{
    for (int x = 0; x <= 9; ++x)
    {
        for (int y = 0; y < 9; ++y)
        {
            if (sudoku[x][y] == 0)
            {
                *row = x;
                *col = y;
                return true;
            }
        }
    }
    return false;
}

然后您可以称其为

if (!find_empty_box(grid, &row, &col)) {
    ... could not find an empty box, done?

这带来了另一个问题——您想传递 grid(不清楚其他地方的 sudoku 是什么),但它是一个数组,所以它实际上是通过引用传递的(隐式指针),因此您在函数中所做的任何更改都会影响调用者的 grid。这可能是也可能不是你想要的。您对 sudoku[&x][&y] 的引用完全是胡说八道——它们很可能会崩溃并且不会做任何有用的事情。