The function is returning a pointer to strb, which will no longer be valid after the function returns. There are 2 main ways you can return a string. One way is to allocate the memory for the string dynamically in the function eg:
char *strb = new char[1024];orchar *strb = malloc(1024);
This isn't very satisfactory though, because you have to deallocate a string which you havn't explicitly allocated eg:
char *string;string = Backwards_String("hello world");//use the string herefree(string); //or delete [] string; depending on how the function allocated
The second way is to pass a pointer to a string buffer to the function as one of it's arguments, then have the function copy the return string into this buffer:
//calling the function with a return bufferchar string[1024];Backwards_String("hello world", string);//the functionvoid Backwards_String(char *stra, char *strb){ //exactly the same as your's, but strb has already been allocated}
(note that I used char *, which is the same as char [], when passing a string. Both pass a pointer to the first character.)
As to which is better, both are used by library functions. A disadvantage of the second method is that to be safe, an int should also be passed specifying the size of the buffer so you're function doesn't overrun it, so it can get a bit untidy passing so many args. The first method can make more sense if the function is obviously going to allocate a new string, like in strdup().
[edited by - frankd on August 26, 2002 7:59:11 AM]