Passing a 2D array to a function

I have a 2D array, "someArray" I'm trying to pass to a function. This function needs to iterate through the 2D array, treating the second dimension as a single-dimension array of its own.

So I declare the array:

int** someArray = new int*[3];

for(int i = 0; i < 3; i++)
    someArray[i] = new int[size];

Then the function:
void someFunction(int someArray[][])
    //The goal here is to pass the second dimension of the array to another 
    //function, as a single dimension array.

void someOtherFunction(int input[])
    //Do stuff with input

(This is pseudo-code meant to simplify the problem, so that I don't have to show and explain my actual code.)

Can anyone help me out here?

I still don't quite understand why so many people use multidimensional arrays -- they're syntax hell.

Anyway what you're asking for in your words and what you're illustrating with code are two different things. In a sense you could say there are two types of 2D arrays:

- "real" 2D arrays in that they're simply an array that takes two indeces. IE: int someArray[3][3];

- pointers to an array of pointers. IE: what you're illustrating with your code.

Pointers to pointers are not really 2D arrays, they're more like layered 1D arrays. Although they're indexed with the same [][] syntax.

ANYWAY, to make a function take a "pointer to a pointer" style 2D array, it's quite simple:

void someFunction(int** someArray)
  someArray[y][x] = example;

Working with "real" 2D arrays is a bit tricker because the size of all but the first brakets must be known:

void someFunc(int someArray[][3])

You cannot omit that [3] -- nor can you pass an array of a different size. IE: int foo[3][4]; could not be passed to that function.

The "pointer to a pointer" approach can work with arrays of any size, though.

But note again that these types are not interchangable. If you have a pointer-to-a-pointer type you cannot pass it to a function that takes a "real" type, and vice versa.

EDIT -- fixed a dumb goof


I suppose I should mention that a better approach would be to objectify your arrays in a container class and pass a reference to that container class to a function, rather than passing an array directly.
Last edited on
Thanks so much, this is exactly what I needed!
closed account (S6k9GNh0)
Also, it's not very common that you'll see people pass large multi-dimensional arrays because they're heavy when passing. They usually just pass a pointer to that array. Similar to the pointer of a pointer above.
@ computerquip
Also, it's not very common that you'll see people pass large multi-dimensional arrays because they're heavy when passing.

Can you elaborate on that - I don't get the 'heavy' bit
That's not the case anyway, since they're passed by reference, not by value.

Basically what computerquip was getting at with the "heavy" comment was he was thinking a copy of the entire array would need to be copied and placed onto the stack for the function call. This happens when you pass structs and classes as function arguments -- and entire copy of that struct/class is made and placed onto the stack for the new function. This is "heavy" because it requires a big copy operation and uses a chunk of additional RAM. This is why you typically pass structs/classes either by pointer (MyClass*) or by reference (MyClass&), rather than by value (MyClass).

computerquip sounds like he was thinking that was done with arrays too, but with the syntax in this thread, that isn't the case. The arrays passed in these examples are all passed by reference and so are all quick and don't use much additional RAM. Don't worry about it.
I know arrays are passed by reference - I wasn't sure whether computerquip knew that.
That's why I was asking him to elaborate.

yeah sorry. I didn't see your username, I just assumed you were the OP.
Topic archived. No new replies allowed.