How to pass a multidimensional array to a function?

How to pass multidimensional array of function in C / C ++?

Array sizes unknown at compile time

+7
c ++ c
source share
9 answers

A pointer to the beginning of the array along with dimensions - then arithmetic of the array in the function is the most common solution.

Or use boost

+9
source share

Passing an array is simple, the hard part is accessing the array inside your function. As noted in some other answers, you can declare a function parameter as a pointer, and also pass the number of elements for each dim array.

#define xsize 20 #define ysize 30 int array[xsize][ysize]; void fun(int* arr, int x, int y) { // to access element 5,20 int x = arr[y*5+20]; } fun(array, xsize, ysize); 

Of course, I did not take into account the whole task of allocating an array (since it is not known what its size will be, you cannot use #defines (and some say that they are bad) / p>

+3
source share

Use a vector of vectors, you can transfer a vector.

+1
source share

You can pass a pointer and dimensions, or use std::vector . But the β€œreal” solution has a pattern:

 template <size_t N, size_t M> void foo(int (&pArray)[N][M]); 

This function template takes an array N by M from int, by reference. Note that this is a function template, not a function, so you get a different instance function for each type of array.

+1
source share

I think this is a GCC extension (or a fairly modern C function), but it can be quite convenient:

 void foo(int bar[n][m], int n, int m) {...} 
+1
source share

You can pass a pointer to the original memory cell of your multidimensional array. you must also pass the size of the array, i.e. the limit of each dimension.

ie

 int var [x][y][z]; func (var, x, y, z); 

function definition:

 void func (int*, int, int, int); 
+1
source share

I just summarize the options from other posts.

If the number of dimensions (N as in an N-dimensional array) is unknown, the only way is to use the C ++ multidimensional array class. There are several publicly available implementations from Boost or other libraries. See Martin Beckett's post .

If the number of dimensions is known, but the size of the array is dynamic, see Tom's answer for accessing an array element (converting a multi-index to an element pointer). The array itself must be allocated using malloc or new.

If you are writing a multidimensional array class yourself, you need to know about "String Order" , Column-major- order, etc.

Namely, if the dimension of the array (Size1, Size2, Size3, ..., SizeN) , then:

  • The number of elements in the array (Size1 * Size2 * Size3 * ... * SizeN)
  • Memory required sizeof(value_type) * numOfElements
  • To access an element (index1, index2, index3, ..., indexN) , use
    • ptr[ index1 + (Size1 * index2) + (Size1 * Size2 * index3) + ... ] Assuming the first index of the array is the fastest measurement
0
source share

Section 3.4 on this page presents your question:

http://www.programmersheaven.com/2/Pointers-and-Arrays-page-2

Of course, variable length arrays were not present in C until C99, and as far as I know, they are not present in C ++. In addition, MSVC does not implement / support C99.

0
source share

A simple method is to smooth the array and iterate using dimensions.

 #include <stdio.h> void print_array(int *arr,int row,int col) { int i,j; for(i=0;i<row;i++){ for(j=0;j<col;j++){ printf("%d ",*(arr+i*col+j)); } printf("\n"); } } int main() { int a[2][3] = {{1,0,2},{-1,3,1}}; int b[4] = {1,2,3,34}; print_array(a,2,3); return 0; } 

This method works, but smoothing the array can interfere with compiler optimization, which in turn can lead to slower execution.

0
source share

All Articles