?
Solved

multi dimensional  arrays

Posted on 2003-03-20
1
Medium Priority
?
153 Views
Last Modified: 2010-04-17
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.
0
Comment
Question by:jainen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 30

Accepted Solution

by:
Mayank S earned 60 total points
ID: 8179121
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!

Mayank.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question