By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

a curious questions about return type

P: n/a
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

Apr 21 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Yes it will be illegal since month's scope is, just as you said, only
local.

You can read about returning arrays here:
http://www.eskimo.com/~scs/cclass/int/sx5.html

Apr 21 '06 #2

P: n/a
questions? wrote:

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:Th*************@gmail.com>

Apr 21 '06 #3

P: n/a


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.

--
Er*********@sun.com

Apr 21 '06 #4

P: n/a
"questions?" <un************@hotmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 21 '06 #5

P: n/a
Murkland wrote:
Yes it will be illegal since month's scope is, just as you said, only
local.


Please review the information below.
Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 21 '06 #6

P: n/a
questions? wrote:
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).
Apr 21 '06 #7

P: n/a
Andrew Poelstra wrote:

questions? wrote:
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:Th*************@gmail.com>
Apr 23 '06 #8

P: n/a
Kenneth Brody <ke******@spamcop.net> wrote:
Andrew Poelstra wrote:

questions? wrote:
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
Apr 24 '06 #9

P: n/a
Richard Bos wrote:

Kenneth Brody <ke******@spamcop.net> wrote:
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:Th*************@gmail.com>
Apr 24 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.