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>