Archived

This topic is now archived and is closed to further replies.

Returning a string

This topic is 5586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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];
or
char *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 here
free(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 buffer
char string[1024];
Backwards_String("hello world", string);

//the function
void 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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites