Returning a string

Started by
4 comments, last by jim bob 21 years, 7 months ago
I have a function that prints a string backwards. My declaration looks like this:
  
char *Backwards_String(char []);
  
First off, is that a bad way to pass a string? Should I pass a pointer or something to the string? Am I right in assuming that is passing a copy of the string, or no? I''m slightly confused about that. My function looks like this. I''m not sure if it will work the way I think it will, because I haven''t been able to test it correctly.
  
char *Backwards_String(char stra[])
    char strb[1024] = "";
    int y = 0;

        for (int x = num_of_chars(stra); x < 1024; x++)
        // num_of_chars returns the number of chars in string

        {
             x--;     // adjust to the array

             stra[x] = strb[y];     // swap chars

             y++;
        }

    return(strb);     // not working right?

}
  
This is the error I get:

warning C4172: returning address of local variable or temporary
 
It compiles with warning and errors on execution. Any help on passing and returning strings is appreciated. Thanks so much. After doing some research, I heard some stuff about constants, static variables, and allocating memory. In the end, I was more confused than when I started. I am not worthy of a sig!
I am not worthy of a sig! ;)
Advertisement
just a thought, if you pass a pointer in as the argument, you don''t need a return value (unless you want to keep the original string), i''m not sure which is best i''ll let someone more experienced answer that
quote:
First off, is that a bad way to pass a string? Should I pass a pointer or something to the string? Am I right in assuming that is passing a copy of the string, or no? I'm slightly confused about that.


Passing char[] or char* is semantically the same. This was you'll be passing a copy of the pointer to the first element in the array.

quote:
char strb[1024] = "";


strb preferably to be allocated in the heap.

One last advise, if you're not doing this string manipulation for learning purposes, then i suggest you go STL.

Regards,
IDispatch.

[edited by - idispatch on August 26, 2002 7:42:33 AM]
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]
If you only need one backwards string at a time, you could also declare the local character array as static, i.e.

static char strb[1024] = ""; 


And the stra parameter should be constant, i.e.

const char stra[] 

or
const char *stra 


By the way, the static method is only appropriate for internally-used functions, if you want it to be a public library function, declare it as

char *Backwards_String( const char *stra, char *strb ) 


or even better

char *Backwards_String( char *strb, const char *stra ) 


as the constant parameters are generally declared after the modifiable parameters.
hmm... you could use strdup(), strrev() and free() to finish the job. eg:
#include "stdio.h"#include "string.h"char *Backwards_String(const char *stra){  char *temp = strdup(stra);  strrev(temp);  return temp;}int main(){  char hello[] = "Hello";  char *p = Backwards_String(hello);  printf("%s\n", p);  free(p);  return 0;} 

... no guaranteed that this is error free. Just type it in 40seconds.

"after many years of singularity, i'm still searching on the event horizon"

This topic is closed to new replies.

Advertisement