return a string?

K

Kelvin@!!!

hi:
if i want a function to return me a string, how can I do it?
i know one method is by passing a string pointer to a function.
is there any implementation?

thank you very much
 
M

Minti

Try this prototype


int giveMeString( char *putMyStringHere, int lengthOfMyString);



The important thing is to have your function strictly adhere to
"lengthOfMyString".


The other way is to dynamically allocate the string in the function
itself only. But then such an approach tends to be bugy.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Kelvin@!!! said:
hi:
if i want a function to return me a string, how can I do it?
i know one method is by passing a string pointer to a function.
is there any implementation?
You don't give particularly much info..

const char * foo()
{
return "Hello";
}

/*remember to free returned string when done with it.*/
char *addhello (const char *str)
{
size_t len;
char *newstr ;
len = strlen(str)+ sizeof("Hello ");
newstr = malloc(len);
/*check for error*/
snprintf(newstr,len,"%s %s","Hello ",str);

return newstr;
}
 
K

Keith Thompson

Kelvin@!!! said:
if i want a function to return me a string, how can I do it?
i know one method is by passing a string pointer to a function.
is there any implementation?

There are several approaches, none of them ideal.

You can make the caller pass in a pointer and length. This forces the
caller to guess how much space will be needed, and the function to
decide what to do if it's not enough.

You can return a pointer to a static object. This leaves the size
decision up to the caller, but can still cause problems if the result
won't fit. It also clobbers the result buffer on each call, so the
caller generally needs to copy the result to a safe location.

You can return a pointer to a result allocated by malloc(). This is
probably the most flexible method, but it requires the caller to
free() the result and risks memory leaks.

You *cannot* return a pointer to a local array variable. The array
ceases to exist when the function returns, and you're left with a
dangling pointer. The result is undefined behavior; in the worst (and
most common) case, it will appear to work properly at first, and fail
only at the most inconvenient possible moment.

I once implemented a variant of the static buffer method. The
function declared an array of half a dozen or so static buffers, with
a static index cycling through them. This allowed several results to
be used simultaneously, but the seventh call would clobber the result
from the first call. I'm not sure I'd recommend this; you have to be
as careful as with the single static buffer, but any errors won't show
up as soon.
 
C

CBFalconer

Keith said:
.... snip ...

I once implemented a variant of the static buffer method. The
function declared an array of half a dozen or so static buffers, with
a static index cycling through them. This allowed several results to
be used simultaneously, but the seventh call would clobber the result
from the first call. I'm not sure I'd recommend this; you have to be
as careful as with the single static buffer, but any errors won't show
up as soon.

Implement a circular list. At initialization time you create it as
long as you wish. From then on, any use involves:

list = list->next;
/* use list->body as needed */

and now you can bandy things about as you desire. There is only
one global pointer for list.

--
"I support the Red Sox and any team that beats the Yankees"
"Any baby snookums can be a Yankee fan, it takes real moral
fiber to be a Red Sox fan"
"I listened to Toronto come back from 3:0 in '42, I plan to
watch Boston come back from 3:0 in 04"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top