View Single Post
(#2 (permalink))
Old
Rain is Offline
Private
Points: 482, Level: 5 Points: 482, Level: 5 Points: 482, Level: 5
Activity: 0% Activity: 0% Activity: 0%
Rain is an unknown quantity at this point
 
 
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Jul 2007
December 24th, 2007

You can always create the 2D array off the heap and then pass the address back to the caller and then have the caller use it for whatever but not to forget to free the space when you are done...

Another alternative is to pass in the 2D array as a parameter into the function (by reference) and then filll it and then just leave the function which would maintain the array's content.

You mentioned you are using "C" and not C++ so unfortunately you dont have the luxury of alias nor the new/delete keywords....

I hope i didn't confuse you....because i am not sure you level of what i am talking about maybe i should explain s'more?

It's been a while since i've looked at C but for your 2D array you can pass it back by allocating memory off the systems heap space by using malloc(..) ... my appologies if C uses a different function for this....it's been a while....C++ is more elegant....so in this example:

int *my2DArray;
malloc(my2DArray,sizeof(int)*10);

This will create a 2D Array that can hold 10 integer values.

so here is what your function would look like:

int* MyFunction(void)
{
int *my2DArray = NULL;
malloc(my2DArray,sizeof(int)*10);

/* Do stuff Here */

return my2DArray;
}


void main()
{
int *myArray=NULL;
myArray = MyFunction();

/* Do whatever here */

free(myArray);

}
Reply With Quote