Path: utzoo!attcan!uunet!aplcen!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!cica!iuvax!ux1.cso.uiuc.edu!jason From: jason@ux1.cso.uiuc.edu (Jason) Newsgroups: comp.sys.mac.programmer Subject: Re: Two (2) weenie C questions... Message-ID: <1990Sep6.164746.7215@ux1.cso.uiuc.edu> Date: 6 Sep 90 16:47:46 GMT References: <202759@<1990Aug29> <53100021@uxh.cso.uiuc.edu> Organization: University of Illinois at Urbana Lines: 46 johnsone@uxh.cso.uiuc.edu writes: >/* Written 2:21 pm Aug 29, 1990 by gft_robert@gsbacd.uchicago.edu */ >>2) How does one return a Str255 from a function? THINK complains "can't return >>array from function"? But aren't I returning a pointer to the first (Oth) char >>of the Str255 if I "return myString"? Do I need Str255 *MyProc() if I want to >>pass back any reference to a Str255 at all? >I was going to suggest declaring the function: > char *MyProc() >but I just tried it and it doesn't work. Using this will work: > MyProc( Str255 myString ) >called with: > Str255 aString; >This doesn't give you a function, but it may serve your purpose. In a Think C function, if you make a declaration of the form: type anArray[ A_SIZE ]; the compiler will not allow such an array to be returned to the calling function because the return value would point to the first character of the array--an array whose memory space was allocated on the stack _temporarily_ and which is released when the function returns. Therefore, a pointer returned in this way actually points to an undefined object and is not usable. The above method ( MyProc( Str255 myString ) ) works because the object being passed to MyProc() is declared in the calling function (or in that function's calling function, etc., or globally) giving it a broader "scope" than this occurrance of the function MyProc(). In other words, myString is good for more than just the duration of MyProc(). If you *really* want a function to return an array, its storage can be allocated on the heap using NewPtr() or NewHandle(). This method is unwise, so I won't go into the complications it presents. -- | | |====================| | | | Jason Watts | \ |\ |\ \ | (jazzin@uiuc.edu) |