return array to main

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2009
Posts: 4
Reputation: miniroselit is an unknown quantity at this point 
Solved Threads: 0
miniroselit miniroselit is offline Offline
Newbie Poster

return array to main

 
0
  #1
Mar 20th, 2009
what is the syntax of returning a two dimensional to main function?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,502
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: return array to main

 
0
  #2
Mar 20th, 2009
  1. int** foo()
  2. {
  3. int i;
  4. int** array = malloc(100 * sizeof(int *));
  5. for(i = 0; i < 100; i++)
  6. array[i] = malloc(20 * sizeof(int)); // allocate 20 integers
  7. return array;
  8. }
  9.  
  10. int main()
  11. {
  12. int ** array = foo();
  13. }
Last edited by Ancient Dragon; Mar 20th, 2009 at 7:45 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: return array to main

 
0
  #3
Mar 20th, 2009
for simplification returned array will be of type:
  1. int array[100][20] ;
or u can modify above function to get variable 2D array:
  1. int** foo(int row, int col)
  2. {
  3. int i;
  4. int** array = malloc(row * sizeof(int *));
  5. for(i = 0; i < 100; i++)
  6. array[i] = malloc(col * sizeof(int)); // allocate 20 integers
  7. return array;
  8. }
  9.  
  10. int main()
  11. {
  12. int ** array = foo(100,20);
  13. }
same result.
Last edited by Narue; Mar 20th, 2009 at 12:11 pm. Reason: added code tags
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 4
Reputation: miniroselit is an unknown quantity at this point 
Solved Threads: 0
miniroselit miniroselit is offline Offline
Newbie Poster

Re: return array to main

 
0
  #4
Mar 21st, 2009
Luckychap;
ur solution is realy helpful. Thak you so much...
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: return array to main

 
-1
  #5
May 19th, 2009
Correction:

  1. for(i = 0; i < row; i++)
Last edited by Luckychap; May 19th, 2009 at 7:08 pm.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC