Returning An Array

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

Join Date: Mar 2007
Posts: 3
Reputation: semmem1 is an unknown quantity at this point 
Solved Threads: 0
semmem1 semmem1 is offline Offline
Newbie Poster

Returning An Array

 
0
  #1
Mar 24th, 2007
I am trying to return an array of integers from a method into an array in my main. I know that you cannot pass an array in C, but you can pass a pointer to the array. My problem is though that every time I pass the pointer, I seem to get random numbers that are unrelated to the actual array back when magicsquaresize is less than/equal to 9. When it is less than 16, part of the array back is the actual array in main and when magicsquare is 25 or more, the whole array is actually brought back. I am really confused on why this would be. Here is my code:

  1. //in my main
  2. int* mptr = initSquare (magicsquaresize ) ;
  3.  
  4. printf("\n");
  5. int i;
  6. for (i = 0; i<magicsquaresize;i++) {
  7. printf( "mptr[%d] = %d\n",i, *mptr);
  8.  
  9.  
  10. //int initSquare, magicsquare is an array of ints that is def correct:
  11. int *mptr = magicsquare;
  12. for (i = 0; i<magicsquaresize;i++) {
  13. printf( "mptr[%d] = %d\n",i,*(mptr+i));
  14. }
  15. return mptr;
Thz for any help anyone can give me.
Last edited by semmem1; Mar 24th, 2007 at 1:29 am.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Returning An Array

 
0
  #2
Mar 24th, 2007
I would suspect you don't have space allocated for the array. How big is the array you defined? Or have you defined just a pointer?

Can't tell from what you've posted.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 3
Reputation: semmem1 is an unknown quantity at this point 
Solved Threads: 0
semmem1 semmem1 is offline Offline
Newbie Poster

Re: Returning An Array

 
0
  #3
Mar 24th, 2007
I am just trying to get the pointers to work so I completely altered my code but it is still not working. I was thinking I didn't allocate enough space, but when I tried that, I just got more errors, as I am not really sure how to allocate space, and malloc didn't seem to be working. My code now gets the 1st and last part of the array right, but the numbers in between still are wrong.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5.  
  6. int main() {
  7. int *bpt = bob();
  8. int i;
  9. for (i = 0; i<9;i++) {
  10. printf( "bPtr[%d] = %d\n",i,*(bpt+i));
  11. }
  12. free(bpt);
  13. } //end of main()
  14.  
  15. int *bob() {
  16. int i;
  17. int num[9];
  18. for (i = 0; i<9; i++) {
  19. num[i] = i+1;
  20. printf("%d ",num[i]);
  21. }
  22. printf("\n");
  23. int *bPtr = num;
  24. for (i = 0; i<9;i++) {
  25. printf( "bPtr[%d] = %d\n",i,*(bPtr+i));
  26. }
  27. printf("\n");
  28. return bPtr;

Thz for any helps guys
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Returning An Array

 
0
  #4
Mar 24th, 2007
Well, no wonder. You're trying to return the address of a local variable, which will go out of scope as soon as the function returns. (Many compilers including gcc even issue a warning regarding this.) Try putting 'static' in front of the variable, which means that the memory allocated will remain the entire life of the program.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 3
Reputation: semmem1 is an unknown quantity at this point 
Solved Threads: 0
semmem1 semmem1 is offline Offline
Newbie Poster

Re: Returning An Array

 
0
  #5
Mar 24th, 2007
Thank You so much for your help. I knew it was something stupid.
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