Passing multidimensional arrays as function arguments in C/C++

This is a topic that has given me tons of headaches, but finally I got the answer.

The simplest way to pass a multidimensional array to a function is by declaring it the same way it was declared in the caller:

void myFunction(int arr[2][2])
{}

int myArray[2][2] = {11,12,21,22};

myFunction(myArray):

Wasn’t that easy?

What you are passing is actually the pointer to the array. Remember that the array’s name is its pointer.

If you want to pass a multidimensional array for which you don’t know its dimensions, then it gets a little bit more complicated. I recommend you to read the sources below.

Sources:

http://www.eskimo.com/~scs/cclass/int/

http://forums.devarticles.com/c-c-help-52/c-pointer-to-multidimensional-array-11075.html

http://stackoverflow.com/questions/4051/passing-multidimensional-arrays-as-function-arguments-in-c

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: