[英]passing statically allocated 2D arrays as function arguments in C
Consider this code: 考虑以下代码:
#include <stdio.h>
#define N 5
void printMatrix(int (*matrix)[N],int n)
{
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n;j++)
printf("%d",matrix[i][j]);
printf("\n");
}
}
int main()
{
int R[N][N]={{1,2,3},{4,5,6},{7,8,9}};
printMatrix(R,3);
}
This works fine as expected. 这按预期工作正常。
Now, I thought to write the functions handling 2D-matrices in a separate source file and link them wherever required. 现在,我想将处理2D矩阵的函数编写在单独的源文件中,并在需要时将其链接。
But then I ran into a problem as in the function printMatrix
, the size of array of int
to which matrix
points (ie N
) is required at compile-time. 但是后来我遇到了一个问题,例如函数
printMatrix
,在编译时需要matrix
指向的int
数组的大小(即N
)。 So, my functions would not work in other cases when the size is different. 因此,当大小不同时,我的函数在其他情况下将不起作用。
So,How can I handle this? 那么,我该如何处理呢?
Dynamic Arrays are a solution but i want to know if it can be done with static arrays. 动态数组是一种解决方案,但我想知道是否可以使用静态数组来完成。
You can't use the built-in 2D array type if both sizes are not known at compile time. 如果在编译时都不知道两个大小,则不能使用内置的2D数组类型。 A built-in 2D array must have at least one of the two sizes known at compile time.
内置2D数组必须至少具有编译时已知的两个大小之一。
If both sizes are run-time values, then you have no other choice but to use a "manual" implementation of 2D array, like an array of pointers to arrays, for example. 如果两个大小都是运行时值,那么您别无选择,只能使用2D数组的“手动”实现,例如,指向数组的指针的数组。 In that case the function declaration might look as follows (two alternative equivalent forms)
在这种情况下,函数声明可能如下所示(两种等效形式)
void printMatrix(int *const *matrix, int n, int m);
void printMatrix(int *const matrix[], int n, int m);
To access to the array elements you can still use the "traditional" syntax 要访问数组元素,您仍然可以使用“传统”语法
matrix[i][j]
The array itself would be created as follows (a simple example) 数组本身将如下创建(一个简单的示例)
int row0[] = { 1, 2, 3 };
int row1[] = { 4, 5, 6 };
int *matrix[2];
matrix[0] = row0;
matrix[1] = row1;
printMatrix(matrix, 2, 3);
But if you already have a matrix implemented as a built-in 2d array 但是,如果您已经将矩阵实现为内置的2d数组
int matrix[2][3] = { ... };
then just to be able to pass it to the above function you can "convert" it into the above form by using an additional temporary "row pointer" array 然后只需将其传递给上述函数,就可以使用附加的临时“行指针”数组将其“转换”为上述形式
int *rows[2];
rows[0] = matrix[0];
rows[1] = matrix[1];
printMatrix(rows, 2, 3);
Write yourself a macro: 给自己写一个宏:
#define MAT(i,j) matrix[i*n + j];
and declare "matrix" as a simple pointer to an "int". 并声明“矩阵”作为指向“ int”的简单指针。
Calculate the array index yourself. 自己计算数组索引。 This will handle an arbitrary two dimensional array, for example:
这将处理任意二维数组,例如:
void printMatrix(int *matrix,int n, int m)
{
int i,j;
for(i=0;i<n;i++){
for(j=0;j<m;j++)
printf("%d",matrix[m * i + j]);
printf("\n");
}
}
Don't try to pass it as a 2-D array; 不要尝试将其作为二维数组传递; pass a pointer to the first element, then compute offsets manually:
将指针传递到第一个元素,然后手动计算偏移量:
void printMatrix(int *a, size_t m, size_t n)
{
size_t i,j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("a[%lu][%lu] = %d\n",
(unsigned long) i,
(unsigned long) j,
a[i*n+j]); // treat a as 1-d array, compute offset manually
}
}
}
int main(void)
{
int arr[5][4];
...
printMatrix(&arr[0][0], 5, 4);
...
}
Granted, this will only work for contiguously allocated arrays. 当然,这仅适用于连续分配的数组。
Although the syntax is not exactly the same, but this also happens to work a bit: 尽管语法不完全相同,但这也可以正常工作:
#include <stdio.h>
#define N 5
void printMatrix(int* row,int n,int sz)
{
int i,j;
int *currRow;
for(i=0;i<n;i++){
currRow = row+i*sz;
for(j=0;j<n;j++)
printf("%d",currRow[j]);
printf("\n");
}
}
int main()
{
int R[N][N]={{1,2,3},{4,5,6},{7,8,9}};
printMatrix(R[0],3,sizeof(R[0])/sizeof(int));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.