问题
I have a 2 dimensional array dynamically allocated in my C code, in my function main. I need to pass this 2D array to a function. Since the columns and rows of the array are run time variables, I know that one way to pass it is :
-Pass the rows and column variables and the pointer to that [0][0] element of the array
myfunc(&arr[0][0],rows,cols)
then in the called function, access it as a 'flattened out' 1D array like:
ptr[i*cols+j]
But I don't want to do it that way, because that would mean a lot of change in code, since earlier, the 2D array passed to this function was statically allocated with its dimensions known at compile time.
So, how can I pass a 2D array to a function and still be able to use it as a 2D array with 2 indexes like the following?
arr[i][j].
Any help will be appreciated.
回答1:
#include <stdio.h>
#include <stdlib.h>
void doit(int ** s , int row , int col )
{
for(int i =0 ; i <col;i++){
for(int j =0 ; j <col;j++)
printf("%d ",s[i][j]);
printf("\n");
}
}
int main()
{
int row =10 , col=10;
int ** c = (int**)malloc(sizeof(int*)*row);
for(int i =0 ; i <col;i++)
*(c+i) = (int*)malloc(sizeof(int)*row);
for(int i =0 ; i <col;i++)
for(int j =0 ; j <col;j++)
c[i][j]=i*j;
doit(c,row,col);
}
Hope this is the way you want ....
回答2:
If your compiler supports C99 variable-length-arrays (eg. GCC) then you can declare a function like so:
int foo(int cols, int rows, int a[][cols])
{
/* ... */
}
You would also use a pointer to a VLA type in the calling code:
int (*a)[cols] = calloc(rows, sizeof *a);
/* ... */
foo(cols, rows, a);
回答3:
You really can't do this without changing a lot of code. I suggest to wrap this in a structure which contains the limits and then use regexp search'n'replace to fix the accesses.
Maybe use a macro like AA(arr,i,j) (as in Array Access) where arr is the structure.
回答4:
As far as I know, all you can pass to a function is a pointer to the first element of an array. When you pass an actual array to a function, it is said that "the array decays into a pointer" so no information about the size(s) of the pointed array remains.
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.
I believe you will be able to find more information about this on the C FAQ.
回答5:
Numerical recipes in C (a book dealing mostly in matrices) suggests a way to do this cleanly and still deal with 2D matrices. Check out the section 1.2 "Matrices and 2D arrays" and the method convert_matrix().
Essentially what you need is to allocate a set of pointers to point to your row vectors. And pass this to your function along with the number of rows and columns.
回答6:
A 2-d array in C is just an array of arrays. Here ya go:
void with_matrix(int **matrix, int rows, int cols) {
int some_value = matrix[2][4];
}
int main(int argc, char **argv) {
int **matrix;
... create matrix ...
with_matrix(matrix, rows, cols);
}
回答7:
int cols = 4;
int rows = 3;
char** charArray = new char*[rows];
for (int i = 0; i < rows; ++i) {
charArray[i] = new char[cols];
}
// Fill the array
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
charArray[i][j] = 'a';
}
}
// Output the array
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << charArray[i][j];
}
cout << endl;
}
来源:https://stackoverflow.com/questions/3414827/how-to-pass-a-2d-dynamically-allocated-array-to-a-function