0
int func(int arr[100][150], int rows, int columns);
int func(int arr[100][150], int rows, int columns)
{
   //stuff here
}

This function works. What should I do if I would like to assign arr size to arr[rows][columns], but not 100 and 150 all the time? If I assign it to 100 and 150 it probably uses more memory than it should if rows and colums are smaller?

int func(int arr[rows][columns], int rows, int columns);
int func(int arr[rows][columns], int rows, int columns)
{
   //stuff here
}

or

int func(int arr[][], int rows, int columns);
int func(int arr[][], int rows, int columns)
{
   //stuff here
}

doesn't work.

good_evening
  • 21,085
  • 65
  • 193
  • 298
  • Your question seems to be a possible duplicate of this one: http://stackoverflow.com/questions/4051/passing-multidimensional-arrays-as-function-arguments-in-c – jrd1 Oct 15 '12 at 15:55
  • You can find detail regarding sending 2D array to a function here. http://stackoverflow.com/questions/12652598/2d-array-as-argument-to-function/12653518#12653518 – Coding Mash Oct 15 '12 at 16:08
  • @jrd: Not a good duplicate, as all of the answers fail to indicate that C supports variable-length arrays in function parameters. – Eric Postpischil Oct 15 '12 at 16:08

2 Answers2

2

Pass the dimensions first:

int func(int rows, int columns, int arr[rows][columns])
{
    …
}

(Actually, the first dimension may be omitted. Dimensions after the first are needed to compute addresses.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • @hey This is a `gcc` extension, not a standard ([link](http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html)). – Sergey Kalinichenko Oct 15 '12 at 16:01
  • wow, it actually does work. so it allocates only memory needed? – good_evening Oct 15 '12 at 16:01
  • @dasblinkenlight I thought a forward decl was a gcc extension, but isn't straight-up params part of C99 ? – WhozCraig Oct 15 '12 at 16:03
  • @dasblinkenlight: This is standard C. It is explicitly shown in C 2011 6.7.5.2 10, “EXAMPLE 4”. – Eric Postpischil Oct 15 '12 at 16:03
  • does it allocate only memory needed? – good_evening Oct 15 '12 at 16:05
  • @hey: An array parameter in a function declaration does not allocate space. It merely provides a way to access the array passed to the function. The calling function (or some ancestor) would allocate space, and that space could be allocated with a similar variable-length array. Allocation could be automatic (“on the stack”), static, or dynamic (with malloc or something similar). – Eric Postpischil Oct 15 '12 at 16:05
  • I mixed up standard versions. That should be C 1999 6.7.5.2 or C 2011 6.7.6.2. It is paragraph 10, “EXAMPLE 4” in both. – Eric Postpischil Oct 15 '12 at 17:20
  • @hey: no new memory is being allocated; `arr` is being treated as a pointer, not an array type. See [this answer](http://stackoverflow.com/questions/12881667/how-to-pass-a-method-the-height-and-width-of-a-2d-array-as-parameters/12882457#12882457) for more details. – John Bode Oct 15 '12 at 17:24
0

Eric's code works, another way to do it (which I prefer) is to replace the explicit array parameter with a pointer like so:

int func(int* x, int rows, int columns)
{
        //...
}

In this way, the function could be used for both 1D and 2D arrays. This is just an alternative to the answer Eric gave; either one could be used, depending on personal preference and the specific application.

Zak Gambrell
  • 45
  • 1
  • 5