问题
I want to pass a 2d array i defined using malloc to a function. First i define the array using code from a blog post.
int** Make2DIntArray(int arraySizeX, int arraySizeY) {
int** theArray;
theArray = (int**) malloc(arraySizeX*sizeof(int*));
for (int i = 0; i < arraySizeX; i++)
theArray[i] = (int*) malloc(arraySizeY*sizeof(int));
return theArray;
}
int main(){
int** myArray = Make2DIntArray(nx, ny);
}
I can then use it as myArray[i][j]. After that,i want to pass this array to a function.I tried to pass it like this:
function(myArray); //function call
//function
void function(int myArray[][]){
//function code goes here
}
but this is wrong.The problem is that the size of the array is different every time.I also tried to define a maximum size for the columns of the array and use it like this:
#define COLUMNMAX 100
function(myArray); //function call
//function
void function(int myArray[][COLUMNMAX]){
//function code goes here
}
but i got the error:
type of formal parameter 1 is incomplete.How can i pass it?
回答1:
The parameter is a int**
And you don't have to cast the return of malloc, just check if it's not NULL
But since you use malloc don't use free when you don't need it anymore
回答2:
myArray
bounds are not know so you should change void function(int myArray[][]){
to void function(int **myArray){
.
Do not make typecasting of malloc
回答3:
The C standard supports variable-length arrays, so you do not need to allocate, assign, or use extra pointers. Simply specify the dimensions you want, as shown below.
Allocate an array of M rows of N columns like this:
int (*array)[N] = malloc(M * sizeof *array);
Declare a function that is receiving such an array like this:
void function(size_t M, size_t N, int array[][N]);
Pass the array to the function like this:
function(M, N, array);
回答4:
What you do with Create2DIntArray
is not an actuall 2d array. It's just an array of pointers, each pointing to some place in memory. And every piece of memory is allocated separately with separate malloc
call (BTW, don't forget to free
it). So basically there's no guarantees, that there bits are allocated consequentionally and properly alligned as with int myArray[][]
. The only way to pass your data as a 2d array, I guess, would be to create a real 2d array and refill it with your data.
回答5:
To get rid of your "type of formal parameter 1 is incomplete." error, you should declare your function before you use it (forward declaration):
void functionB(int **myArray); //forward declaration
void functionA() {
functionB(blaaaa); //usage
}
void functionB(int **myArray) {
//implementation
}
回答6:
It looks like what you're needing here is some booking information for the size of the rows and columns in the array. There are multiple ways to do that, but here are four)
1) If you know the size at compile time, you could use a define like you tried to do. Defines are globally scoped, you can access them in all your functions in a file. Instead of somehow trying to pass the defined value (like COLUMN_SIZE ) simply use it as a for loop end-condition inside your "function" function. However, if you know the size before compile time, you might as well simply make an array of fixed size. Malloc is usually meant for dynamic run-time determined memory allocation.
2) Simply store the values of nx and ny in main, and make it so that "function" takes 3 arguments: (int[][] my_array, column_size, row_size) and pass it your pointer to the array as well as nx and ny. This is preferable than method 1, because it allows for an array size to be specified during runtime, not during compile time.
3)This is the one I would recommend: Use a structure, like this
struct 2d_array{
int row_size;
int column_size;
int** actual_array;
}
With this, you have a few options. You could edit Make2dIntArray so that it returns either a 2d_array structure or a pointer to a 2d_array structure (depending on if you want to handle the structure with pass by reference or pass by value). Or you could have it so that you construct the structure in main. I'd advise having Make2dIntArray return the structure, it would be very clean.
And then you could have your "function" function simply take a 2d_array structure as it's argument (again, either as a pointer or not, depending on if you want to change the row_size and column_size variables, which I can't see any purpose for unless you remalloc actual_array). Then you just access the row_size and column_size variables in the structure inside "function" and you have all the information you need.
Basically, the whole idea behind this is book-keeping. Keep track of the information you need, and make it easily accessible when you need it.
来源:https://stackoverflow.com/questions/17236064/how-to-define-a-2d-array-using-malloc-and-pass-it-to-a-function