0

Is it possible to do this

std::string str(const char* s)
{  
     return std::string(s);
} 

int main() {  
    char* strz = (char*)str("asd").c_str();  
}  

Instead of:

int main(){  
    std::string temp = str("asd");  
    char* strz = (char*)temp.c_str();  
}

I know it should be const char* strz but I need it only within block of code(and without new/delete). After returning string from method it look for reference(if cant find it, deletes string) and then calls c_str(). I have a lot of char's(independent from me) and I could use second solution but it takes too much code.

4
  • Please indent your code 4 spaces instead of marking every single line as code using the inline code syntax (``)! Commented Apr 22, 2012 at 17:00
  • ^ i.e. highlight all of it and press ctrl-K or hit the code format button Commented Apr 22, 2012 at 17:03
  • Your str function is unneccesary. Do you have it just to shorten the code? Commented Apr 22, 2012 at 17:16
  • @Zyx2000 no, it's just illustrative, practically I'm using it to convert text between MBCS and Unicode Commented Apr 22, 2012 at 17:25

3 Answers 3

6

If you use your second option -

std::string temp = str("asd");
char* strz = (char*)temp.c_str();

You're possibly running into undefined behavior. It's illegal to modify the contents of strz. You'll have to use strcpy to get a mutable array of char's. Your first isn't any better either, moreover, it's redundant, since you can directly use string's constructor.

Anyway, to get a char* from a string you can do:

char* get(const std::string& str)
{
   char* ret = new char[str.length() + 1]();
   strcpy(ret,str.c_str());
   return ret;
}

You'll have to delete[] the memory yourself when you're done with it.

Sign up to request clarification or add additional context in comments.

3 Comments

I need to use this cast only temporarily(not even writeable), later it is copied(but copied from char*)
@user1112008 if it's not writable, why not have a const char*?
As I told it's independent from me :( I could edit structure which holds it on "my side" but on "other side" it will be read as char*. I'm writing extension for existing application(sending is synchronous).
1

There's a problem even beyond the cast from const char* to char*

char* strz = (char*)str("asd").c_str();

This creates a temporary std::string which is destroyed at the end of this line. And that invalidates the char* strz.

It sounds to me like you should just be holding on to the returned std::string rather than treating it as a temporary:

std::string strz = str("asd");
...
foo(strz.data()); // use strz

Comments

1

Let me guess what you wanted to ask and answer it :) Feel free to ignore if i guessed wrong :)

So you have some function that returns std::string:

std::string str(...);

And you have some old code that uses char* (so old that it doesn't even declare its argument as const char*, even though it doesn't modify the string), which you want to use with the result:

int ugly_func1(char*) {...}
int ugly_func2(char*) {...}
int ugly_func3(char*) {...}

int main()
{
    std::string temp = str(...);
    char* temp1 = (char*)temp.c_str();
    ugly_func1(temp1);
    ugly_func2(temp1);
    ugly_func3(temp1);
}

So, no, you must use the syntax with the temp variable. You can't write this:

int main()
{
    char* temp = (char*)str(...).c_str();
    ugly_func1(temp);
    ugly_func2(temp);
    ugly_func3(temp);
}

This is illegal code (e.g. destroying the return-value of str() before calling ugly_func1 on it). The nasty thing is that it may sometimes appear to work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.