| | |
Returning An Array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 3
Reputation:
Solved Threads: 0
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:
Thz for any help anyone can give me.
C Syntax (Toggle Plain Text)
//in my main int* mptr = initSquare (magicsquaresize ) ; printf("\n"); int i; for (i = 0; i<magicsquaresize;i++) { printf( "mptr[%d] = %d\n",i, *mptr); //int initSquare, magicsquare is an array of ints that is def correct: int *mptr = magicsquare; for (i = 0; i<magicsquaresize;i++) { printf( "mptr[%d] = %d\n",i,*(mptr+i)); } return mptr;
Last edited by semmem1; Mar 24th, 2007 at 1:29 am.
•
•
Join Date: Mar 2007
Posts: 3
Reputation:
Solved Threads: 0
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.
Thz for any helps guys
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int *bpt = bob(); int i; for (i = 0; i<9;i++) { printf( "bPtr[%d] = %d\n",i,*(bpt+i)); } free(bpt); } //end of main() int *bob() { int i; int num[9]; for (i = 0; i<9; i++) { num[i] = i+1; printf("%d ",num[i]); } printf("\n"); int *bPtr = num; for (i = 0; i<9;i++) { printf( "bPtr[%d] = %d\n",i,*(bPtr+i)); } printf("\n"); return bPtr;
Thz for any helps guys
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.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- Please helpwith how to return two dimensional array (C++)
- Return Array from C++ (C++)
- returning an array? (C++)
- Calling Oracle Stored Procedures with ASP (ASP)
Other Threads in the C Forum
- Previous Thread: Recrusive Split on a linked list
- Next Thread: sending part of array
| Thread Tools | Search this Thread |
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string strings structures suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






