我编写了这段代码来检查 suduko 答案,由于某种原因,我将 suduko 的平方相加而成的数组在我将其归零的函数中全为零,但是当我在检查函数中使用它时,它不是'全部为零
如果我将数组归零函数的完全相同的代码移至其他函数并运行它,它就可以工作。 (我正在使用c99,不知道这是否重要)
有什么想法吗?
int squareSum[5][5];
//set array elements to zero
setArrayToZero(squareSize, squareSize, squareSum);
/*for(int i = 0; i<squareSize; i++){
for(int j = 0; j<squareSize; j++)
squareSum[i][j] = 0;
}*/
printf("%d, %d\n%d, %d\n\n", squareSum[0][0], squareSum[0][1], squareSum[1][0], squareSum[1][1]);
this is the array is case squareSize is two 如果我在注释中添加 for ,则数组全为零,正如您在下面看到的,它与我调用的函数完全相同。
void setArrayToZero(int rows, int columns, int array[][columns]){
for(int i = 0; i<rows; i++)
for(int j = 0; j<columns; j++)
array[i][j] = 0;\\if i print the array in function its all zeros.
附注 我知道我只使用了数组的一部分,这是大学的作业,我们不允许使用 malloc,所以我以最大大小 - 25 创建数组。
提前谢谢您。
完整的c文件: https://drive.google.com/open?id=1L00L3lvMYNcaz2SswEBnmi9KO-79oaHg
所有的打印功能,都是类(class)需求的一部分(用于自动检查)
最佳答案
A reference to an object of type array-of-T which appears in an expression decays (with three exceptions) into a pointer to its first element; the type of the resultant pointer is pointer-to-T.
因此,这意味着在这种情况下(这不是这 3 个异常(exception)之一),您传递的数组将衰减为指针,现在您通过访问地址更改为数组,这就是为什么它将保留在被调用函数。
你的做法是正确的。问题不在于通过或其他什么。也许您访问它是错误的,或者您没有正确初始化它。但清零并没有什么问题。
更多信息在 C 中一切都是按值传递的。 C 中没有所谓的“按引用传递”。指针的工作原理让我们认为 C 中存在“按引用传递”的东西,但事实并非如此,这里指针变量也被复制到被调用函数中的某个局部变量中。但由于我们在被调用函数中有这些地址,并且我们访问它们并进行更改 - 它们保留在被调用函数中。
<小时/>OP发布示例代码后
与过于复杂的数独检查逻辑相比,错误更多。
我只提一下打印部分。
在 C 语言中,二维数组的元素是按顺序存储的。当我们将二维数组传递给函数时,我们需要指定列大小,以便我们可以确定正确的元素。
假设您要访问 10x13
数组中的 a[4][7]
。该元素位于地址 &a[0][0]+4*13+7
处。这就是 column
部分作为参数传递的原因。
现在你做了什么:
int squareSum[5][5], rowColSum[25][2];
//set arrays elements to zero
setArrayToZero(size, 2, rowColSum);
setArrayToZero(squareSize, squareSize, squareSum);
第一个还可以。因为有 2 列。但第二个呢?
在这里,您告诉函数您正在传递一个列 size = 2
的数组,但事实并非如此。它仍然是 5 列的二维数组。
这就是你遇到问题的地方。假设您使用 10,21,34,14
假设grid
是5x5
数组(在你的情况下是25x25)
网格[5][5]数组
你这样做
for(int i = 0; i<squareSize; i++)
for(int j = 0; j<squareSize; j++)
scanf("%d",&grid[i][j]);
/*
Input is 13 17 19 23
*/
+-----+-----+-----+------+-----+-----+-----+----+----+----+--...\
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+-----+-----+-----+------+-----+-----+-----+----+----+----+--...\
/ \ \ / \ \
13 17 19 23
现在你可以像这样访问它
通过此调用将其传递给函数 print2dArray(int row, int col, int g[][col])
你这样调用 `print2dArray(2,2,grid);`` ...
for(int i = 0; i<row; i++)
for(int j = 0; j<col; j++)
printf("%d",&grid[i][j]);
现在您将打印这些元素(i):表示顺序
+-----+-----+-----+------+-----+-----+-----+----+----+----+--...\
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+-----+-----+-----+------+-----+-----+-----+----+----+----+--...\
/ \ \ ^ ^ / \ \
13 17 | | 19 23
(1) (2) (3) (4)
同样,在您的情况下,这些也未初始化。这就是为什么你会得到那些奇怪的结果。您将二维数组的一些元素初始化为 0,但是当您读取数组时,您正在访问一些不同的元素。这就是错误结果的原因。
解决方案:
调用
int CorrectSum = inputGrid(squareSize, 25, grid);
请注意,它应该是squareSize
。相应地更改inputGrid
中digitsCounter
的存储。inputGrid
的函数签名为inputGrid(size, col, grid[][col]);
调用时相同
setArrayToZero(size, 2, rowColSum);
和setArrayToZero(squareSize, 5, squareSum);
我根本没有检查逻辑。答案涉及解释二维数组的打印行为。
关于c - 将数组元素归零的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47589084/