a curious questions about return type

Q

questions?

char * get_month(double number){
char month[10];
blah blah

return month
};



int main(){

printf("I want to print %s\n",get_month(whatever);

}


Will this be illegal, since the pointer is pointing to the local
variable month which will be released soon?
What's the nice way of returning a string?

Thanks
 
K

Kenneth Brody

questions? said:
char * get_month(double number){
char month[10];
blah blah

return month
};

int main(){

printf("I want to print %s\n",get_month(whatever);

}

Will this be illegal, since the pointer is pointing to the local
variable month which will be released soon?

Not "soon", but "right now".
What's the nice way of returning a string?

One way is to make the buffer static in get_month():

char *get_month(double number) /* Why "double"? */
{
static char month[10]; /* Is this long enough? */
...
return(month);
}

Another method is to pass the buffer to get_month():

char *get_month(double number,char *month)
{
...
return(month);
}

int main()
{
char buffer[10]; /* Is this long enough? */
...
printf("I want to print %s\n",get_month(whatever,buffer));
}

There are other methods, such as malloc() the buffer, but then you have to
make sure to document that the caller needs to free() it.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
E

Eric Sosman

Murkland wrote On 04/21/06 13:48,:
Yes it will be illegal since month's scope is, just as you said, only
local.

The scope isn't a problem; it's entirely possible
to use objects whose identifiers are not in scope. The
trouble comes from the storage duration: an `auto' object
ceases to exist when execution leaves its containing block.
Thus, any pointer to such an object becomes indeterminate
and unsafe to use.
 
K

Keith Thompson

questions? said:
char * get_month(double number){
char month[10];
blah blah

return month
};



int main(){

printf("I want to print %s\n",get_month(whatever);

}


Will this be illegal, since the pointer is pointing to the local
variable month which will be released soon?
What's the nice way of returning a string?

It's not illegal (i.e., the implementation is not obligated to
diagnose it), but any reference to the returned value invokes
undefined behavior.
 
A

Andrew Poelstra

questions? said:
char * get_month(double number){
char month[10];
blah blah

return month
};



int main(){

printf("I want to print %s\n",get_month(whatever);

}


Will this be illegal, since the pointer is pointing to the local
variable month which will be released soon?
What's the nice way of returning a string?

Thanks

Just replace
char month[10];
with
char *month = malloc(10);

Just be sure to #include <stdlib.h>, and to document that the calling
function needs to handle the deallocation (this is important because any
code could become a library if need be).
 
K

Kenneth Brody

Andrew said:
questions? said:
char * get_month(double number){
char month[10];
blah blah

return month
};
[...]

Just replace
char month[10];
with
char *month = malloc(10);

Just be sure to #include <stdlib.h>, and to document that the calling
function needs to handle the deallocation (this is important because any
code could become a library if need be).

And be sure to document that the caller is responsible for free()ing the
returned buffer when done, in order to prevent memory leaks.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
R

Richard Bos

Kenneth Brody said:
Andrew said:
questions? said:
char * get_month(double number){
char month[10];
blah blah

return month
};
[...]

Just replace
char month[10];
with
char *month = malloc(10);

Just be sure to #include <stdlib.h>, and to document that the calling
function needs to handle the deallocation (this is important because any
code could become a library if need be).

And be sure to document that the caller is responsible for free()ing the
returned buffer when done, in order to prevent memory leaks.

Erm... that's what Andrew just said.

You also need to make sure that your function (and its caller) handles a
null return from malloc() cleanly, though.

Richard
 
K

Kenneth Brody

Richard said:
Kenneth Brody said:
Andrew Poelstra wrote: [...]
Just be sure to #include <stdlib.h>, and to document that the calling
function needs to handle the deallocation (this is important because any
code could become a library if need be).

And be sure to document that the caller is responsible for free()ing the
returned buffer when done, in order to prevent memory leaks.

Erm... that's what Andrew just said.
[...]

Umm... Err... Mmm... Well...

*Thwack*thwack*thwack*

Sorry. I feel much better now.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 

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

Forum statistics

Threads
474,177
Messages
2,570,953
Members
47,507
Latest member
codeguru31

Latest Threads

Top