TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: david nugent
from: Steven Pasztor
date: 1993-10-13 18:47:02
subject: Returning a string in C

So to david nugent do I speak these words:


Wednesday October 13 1993 03:37, david nugent wrote to David Woodruff:



 dn> char *
 dn> doSomeStuff (int value)
 dn> {
 dn>     static char myPrivateBuffer[80];
 dn>     sprintf (myPrivateBuffer, "The value is %d", value);
 dn>     return myPrivatebuffer;
 dn> }
 dn> (note the 'static' in the declaration of MyPrivateBuffer - it is an error
 dn> to return a pointer to a stack object since it no longer exists after the
 dn> function exits)

I may be wrong here... but it seems that with something like this, you
wouldn't be able to have space already set aside for the destination of the
string, since you'd loose the pointer when it got overwritten by the value
returned by the function. You'd have to transfer the string from the
address returned by the funtion, into the location you want.

The second example (which I removed) avoids this by getting you to pass a
pointer to the string. Is there any way of finding out where the returned
value will be going? This would allow you to write directly to the
destination of the string, avoiding the problem of having to copy the
string latter on. And without making it neccisary to supply the name of the
string.


nevets


... C program run.  C program crash.  C programmer quit.

--- FMail 0.94

* Origin: HELP!!! (3:632/103.123)
SEEN-BY: 50/99 54/54 99 620/243 622/405 623/630 632/103 301 348 365 386 998
SEEN-BY: 633/371 634/383 384 635/502 503 544 555 570 636/100 670/206 711/401
SEEN-BY: 711/409 430 807 808 809 932 934 712/623 627 713/888 714/906 800/1
@PATH: 632/103 348 635/503 50/99 54/54 99 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.