[英]How do I pass a pointer to a multidimensional array in C?
I have a two dimensional array that works with a function: 我有一个二维数组,可以使用一个函数:
bool matrix[rows][cols];
func(rows, cols, matrix);
void func(int rows, int cols, bool matrix[rows][cols]) {
//...
}
However, as soon as I try to have matrix
in the original function modified by: 但是,只要我尝试将原始函数中的
matrix
修改为:
bool matrix[rows][cols];
func(rows, cols, &matrix);
void func(int rows, int cols, bool *matrix[rows][cols]) {
//...
}
I receive an incompatible pointer type error. 我收到一个不兼容的指针类型错误。 I am clueless as to why.
我对此无能为力。
bool matrix[rows][cols]
is an array of arrays of a type bool
bool matrix[rows][cols]
是bool
类型的数组数组
bool* matrix[rows][cols]
is an array of arrays of a type pointer to bool
or simply bool*
. bool* matrix[rows][cols]
是一个指向bool
或简单bool*
的类型指针数组的数组。
Thus if you defined your function to take an array of arrays of type bool*
, you need to pass that type: 因此,如果您将函数定义为采用
bool*
类型的数组数组,则需要传递该类型:
bool* m[row][col];
func( row , col , m );
If you want to have a pointer to bool matrix[rows][cols]
, then your approach is not correct. 如果你想要一个指向
bool matrix[rows][cols]
的指针,那么你的方法是不正确的。
A pointer to matrix has the type: bool (*pmatrix)[rows][cols]
. 指向矩阵的指针的类型为:
bool (*pmatrix)[rows][cols]
。 So define your function with that type and pass the address of the matrix array: 因此,使用该类型定义函数并传递矩阵数组的地址:
func( rows , cols , &matrix );
@2501 has already answered your question , but, since you want the modified array to be reflected to the main function, you don't actually need a pointer to the array (which will complicate things more)! @ 2501 已经回答了你的问题 ,但是,既然你想要将修改后的数组反映到main函数中,你实际上并不需要指向数组的指针(这会使事情复杂化)! Just pass the array directly as you'll get the expected results!
只需直接传递数组,就可以得到预期的结果!
Why, you ask? 你为什么问?
Short answer: In C, arrays are passed by reference. 简答:在C中,数组通过引用传递。
Long answer: 答案很长:
Always keep in mind that when you use the name of an array, it gets converted to a pointer to its first element † . 请记住, 当您使用数组的名称时,它会转换为指向其第一个元素 † 的指针 。 This is commonly referred to as " array decay ".
这通常被称为“ 阵列衰减 ”。
Coming back to your code, The diagram of bool matrix[rows][cols];
回到你的代码,
bool matrix[rows][cols];
图bool matrix[rows][cols];
would be: 将会:
+---------------------+---------------------+---------------------+---------------------+---------------------+
| | | | | |
| matrix[0][0] | matrix[0][1] | matrix[0][2] | ... | matrix[0][cols - 1] |
| | | | | |
+---------------------+---------------------+---------------------+---------------------+---------------------+
| | | | | |
| matrix[1][0] | matrix[1][1] | matrix[1][2] | ... | matrix[1][cols - 1] |
| | | | | |
+---------------------+---------------------+---------------------+---------------------+---------------------+
| | | | | |
| ... | ... | ... | ... | ... |
| | | | | |
+---------------------+---------------------+---------------------+---------------------+---------------------+
| | | | | |
| matrix[rows - 1][0] | matrix[rows - 1][1] | matrix[rows - 1][2] | ... | matrix[rows - 1][cols - 1] |
| | | | | |
+---------------------+---------------------+---------------------+---------------------+---------------------+
From the above diagram, it is clear that the first element of 从上图中可以清楚地看到第一个元素
bool matrix[rows][cols];
is the first subarray matrix[0][0]
to matrix[0][cols - 1]
. 是第一个子阵列
matrix[0][0]
到matrix[0][cols - 1]
。 So what happens here is that the address of this subarray is being passed to the function . 那么这里发生的是这个子阵列的地址被传递给函数 。 This is of type
bool (*)[cols]
. 这是
bool (*)[cols]
。 This would mean that 这意味着
void func(int rows, int cols, bool matrix[rows][cols])
would work the same way as 会像以前一样工作
void func(int rows, int cols, bool (*matrix)[cols])
So, for example, if you wanted to write to the third slot of the second subarray of matrix
, you can use matrix[1][2] = WHATEVER;
因此,例如,如果要写入
matrix
的第二个子阵列的第三个槽,可以使用matrix[1][2] = WHATEVER;
and the changes made from the function would also affect to the caller since the address was passed. 并且自该地址通过以来,该函数所做的更改也会影响调用者。
† : There are a few exceptions where array "decay" doesn't occur. † :有一些例外情况,阵列“衰变”不会发生。 See Exception to array not decaying into a pointer?
请参见异常数组不衰减成指针?
A pointer to a single dimensional array, say int a[10]
may look like below: 指向单维数组的指针,比如
int a[10]
可能如下所示:
int (*ptr)[10]
| |______________array of 10 integers(read type and number together)
|______a pointer to ^
A pointer to a multi dimensional array, say int a[10][10]
may look like below: 指向多维数组的指针,比如
int a[10][10]
可能如下所示:
int (*ptr)[10][10]
| | |_________________array of 10 integers(read type and number together)
| |______________array of ^
|______a pointer to ^
Warning: Mind the parenthesis. 警告:注意括号。
*matrix[rows][cols])
is different from (*matrix)[rows][cols])
. *matrix[rows][cols])
不同于(*matrix)[rows][cols])
。 The difference is pointed out in the answer by @2501. @ 2501在答案中指出了不同之处。
You can use a ***
for a matrix. 您可以使用
***
作为矩阵。
char ***matrix = alloc_matrix(BUFFER_SIZE, BUFFER_SIZE);
char ***alloc_matrix(unsigned rows, unsigned columns) {
char ***matrix = malloc(rows * sizeof(char **));
if (!matrix) abort();
for (unsigned row = 0; row < rows; row++) {
matrix[row] = calloc(columns, sizeof(char *));
if (!matrix[row]) abort();
for (unsigned column = 0; column < columns; column++) {
matrix[row][column] = NULL;
}
}
return matrix;
}
Here is an example where we use malloc
and pointers
to create a matrix. 这是一个使用
malloc
和pointers
创建矩阵的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.