Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

multi dimensional  arrays

Posted on 2003-03-20
1
Medium Priority
?
154 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
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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

580 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