943,972 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 957
  • C RSS
Mar 24th, 2007
0

Returning An Array

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
semmem1 is offline Offline
3 posts
since Mar 2007
Mar 24th, 2007
0

Re: Returning An Array

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.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,744 posts
since May 2006
Mar 24th, 2007
0

Re: Returning An Array

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
semmem1 is offline Offline
3 posts
since Mar 2007
Mar 24th, 2007
0

Re: Returning An Array

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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 24th, 2007
0

Re: Returning An Array

Thank You so much for your help. I knew it was something stupid.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
semmem1 is offline Offline
3 posts
since Mar 2007

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: Recrusive Split on a linked list
Next Thread in C Forum Timeline: sending part of array





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


Follow us on Twitter


© 2011 DaniWeb® LLC