How do I cast from std:string to char *?
Thanks
avi123
Printable View
How do I cast from std:string to char *?
Thanks
avi123
You can't do that. The only thing you can get from an std::string is a const char *, which means that you can't modify it. You get this by calling string::c_str()
can I convert const char* to char* ?
I didn't mean the last question seriously..
The "const char *" means that you can't modify it.
Are you wanting to modify the string value by manipulating
the buffer directly??
What is the reason that you are wanting the "char *" instead
of the "const char *"?
You can, but it's very dangerous. The way to do it is to use const_cast as described in this FAQ item. You should not modify the buffer directly, but there may be valid reasons to do the cast. One that springs to mind is when you want to call a C-style function (from Windows, another dll etc.) that will basically never change the string, but doesn't have the const in its declaration.Quote:
Originally posted by avi123
can I convert const char* to char* ?
I need to be able to work on this string but it doesn't have to be the returned string, so I guess I can put it on a new buffer of char * type is that right?
I mean can I do:
const char* str1;
char* str2;
str2 = (char*)str1
is that ok?
Don't change the size of the string.
another question:
I used string::c_str() as suggested at the compilation passed thought I tried to changed the return value, is that ok?
I mean I did:
const char * string= stdString.c_str();
and the compilation passed when tring to change string
No that is not OK. The (char *) is a c-style cast that removes the constness of str1. C-style casts should not be used anymore (OK, I do sometimes use them) but here it's basically a const_cast. And as I have pointed out above, it's a dangerous thing to do.Quote:
Originally posted by avi123
I need to be able to work on this string but it doesn't have to be the returned string, so I guess I can put it on a new buffer of char * type is that right?
I mean can I do:
const char* str1;
char* str2;
str2 = (char*)str1
is that ok?
If you want to modify it, you'll have to make a non-const copy of the string.
const is there for a reason. The reason is that you should not modify a const object. If you try to modify it, it could lead to all sorts of strange problems and undefined behaviour.Code:string s = "Hello";
char *sz;
sz = new char[s.length() + 1];
strcpy(sz, s.c_str());
// now work with sz, which is a copy of s
// clean up
delete [] sz;
That's a bug in your compiler. It should not allow changing a const object.Quote:
Originally posted by avi123
another question:
I used string::c_str() as suggested at the compilation passed thought I tried to changed the return value, is that ok?
I mean I did:
const char * string= stdString.c_str();
and the compilation passed when tring to change string
Another thing is that you should not call your variable string, since string is already std::string.
Compiling the following on VC.NET gives me an error (as it should):
And the error is pretty descriptive:Code:string s = "Hello";
const char *sz = s.c_str();
sz[0] = 'B';
Error: l-value specifies const object
well I don't know what was the problem but bow I got the error which is good.
How do I cast in the different direction?
I mean I have a char* (for example "1234567890")
and I want to put in a std:string?
Thanks again
Is this what you mean?
Code:char *s1 = "123456";
string s2 = s1;
yep
is that also ok?
char *s1 = "123456";
string s2(s1);
Yes, that's OK.