View Single Post
Old 05-11-2002, 10:12 PM   #2
Yui Unifex
Senior Member
 
Join Date: Apr 2002
Location: Florida
Posts: 323
Yui Unifex is on a distinguished road
Send a message via ICQ to Yui Unifex Send a message via AIM to Yui Unifex
Question

Your problem is just that -- you're returning the address of a local variable.  Every variable in a function is only local to that function -- if you tried to return the address of it, it would be invalid.  So, your compiler is warning you that you shouldn't do this.  Your problem arises because "char buf[256];" is actually "char *buf", which is a pointer, or an address.

You can solve this one of two ways: You can call str_dup() on the string, but you must remember to call free_string() on it!  Or, you can make the local variable static, which means that it stays alive even when you're not in the function.  To make the variable static, all you'd have to do is place "static char buf[256];" instead of the current line.

One quick design caveat: You should really return most strings as const.  I know alot of Dikurivatives don't do this, and it forces you to break const correctness elsewhere, but it's a bad practice that should be avoided.
Yui Unifex is offline   Reply With Quote