how do I return a 2D array from a function in C? - KingPin's Forum
KPsDenKPsDen ArmoryImage HostingLegendz text RPGIp DisplayIRCFlash Arcade
Forum
      |        
Register
         Double dragon (Posted By:The_FreeMan - Replies:0 - Views:16)      « »     [epic] post you pic thread [/epic] (Posted By:Ego - Replies:323 - Views:7741)      « »     no wai! (Posted By:shek - Replies:15 - Views:196)      « »     Mangos : Creature, Items, Spells & object ID's (Posted By:KingPin - Replies:36 - Views:27068)      « »     Insurgency Server (Posted By:KingPin - Replies:8 - Views:191)      « »     [Check4SPAM] RE: Top 10 Most Popular iPod Video Tools (Posted By:Isabell - Replies:0 - Views:1)      « »     Leather conversions via .tradein (Posted By:vohrtechs - Replies:18 - Views:509)      « »     Infraction for wowgolds987: Repeated Rule Violaion (Posted By:Texan - Replies:0 - Views:1)      « »     Infraction for wowgolds987: Repeated Rule Violaion (Posted By:Texan - Replies:0 - Views:1)      « »     [Check4SPAM] RE: Re: wow gold (Posted By:wowgolds987 - Replies:0 - Views:1)      « »     
KingPin's Forum
 
K.P.s.N. Register vbBux / KPs Mall Bugs Blogs FAQ Search Today's Posts Mark Forums Read Donate
Go Back   KingPin's Forum > KP's Network Forum > RSS News
Reload this Page how do I return a 2D array from a function in C?
 


RSS News This is a discussion on how do I return a 2D array from a function in C? in the RSS News;
Description: do i need to use pointers/struct in order to make it easier? If possible, a straightforward and simple way would ...

Reply
 
LinkBack Thread Tools
how do I return a 2D array from a function in C?
(#1 (permalink))
Old
kerocute3 is Offline
Private
Points: 167, Level: 2 Points: 167, Level: 2 Points: 167, Level: 2
Activity: 0% Activity: 0% Activity: 0%
kerocute3 is an unknown quantity at this point
 
kerocute3
Rupees: 86.00
Bank: 500.00
Total Rupees: 586.00
 
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Dec 2007
how do I return a 2D array from a function in C? - December 24th, 2007

do i need to use pointers/struct in order to make it easier? If possible, a straightforward and simple way would do... thanks!
sample codes will be highly appreciated... thanks!
and also, i need it to be passed by value since the function is recursive.

 
Reply With Quote
Revenue Shared Ads
(#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
 
Rain
Rupees: 1.00
Bank: 500.00
Total Rupees: 501.00
 
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
Revenue Shared Ads
Reply

Bookmarks

Tags
array, function, return

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Rupees Per Thread View: 1.00
Rupees Per Thread: 15.00
Rupees Per Post: 5.00
Forum Jump



Powered by vBulletin® Version 3.8.0 Beta 1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
Copyright 2004-2009 KPsN


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81