943,735 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1521
  • C RSS
Mar 20th, 2009
0

return array to main

Expand Post »
what is the syntax of returning a two dimensional to main function?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
miniroselit is offline Offline
4 posts
since Jan 2009
Mar 20th, 2009
0

Re: return array to main

  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Mar 20th, 2009
0

Re: return array to main

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
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Mar 21st, 2009
0

Re: return array to main

Luckychap;
ur solution is realy helpful. Thak you so much...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
miniroselit is offline Offline
4 posts
since Jan 2009
May 19th, 2009
-1

Re: return array to main

Correction:

  1. for(i = 0; i < row; i++)
Last edited by Luckychap; May 19th, 2009 at 7:08 pm.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: decoding auto-generated header #defines
Next Thread in C Forum Timeline: Hi,Just tell where i am wrong.No need to solve.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC