multi dimensional arrays

i have written a function, that is stores numbers in a 2 dimensional array and i want to return this array to the main, but i don't know what type to specify the function as, i have tried typedef and 2 dimensional arrays, but the compiler gives error messages and it still doesn't work. could anyone help out please.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
Since you're using the term 'function' and also talking about main (), I will assume that you're working with C/C++.

You can use char ** (as many *'s as the dimension of the array). However, it is not very advisable because in case you are returning a local 2-Dimensional array (local to the function, I mean), then by the time you are back in main (), the memory for that array has already been deallocated (since after function-execution, the memory for all its local variables gets deallocated). So in main (), your pointer-to-a-pointer (which will receive the 2-D array), will effectively be pointing to garbage. Remember, you cannot hold the returned address in an array because you cannot attempt to change the base address of an array. You can only hold returned addresses in pointer variables.

It is better to pass the 2-D array as an argument, since an array is always passed by address, and so, any modifications made to it in the function are permanent, and so, you don't even need to return anything.

Something like:

void function ( int array[][10], int size )
  .. // do your stuff

} // end of function ()

void main ()
  int a[5][10] ;
  function ( a, 5 ) ; // function-call

} // end of main ()

Hope that helps!


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.