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.
Well...to go even further and to point it out clearly. You should never ever cast away the 'const' of the character string returned by a call to 'c_str()'. First of all, the supplier of the function may change it in the way that it will change the string. The main reason is simply that it is not guaranteed that the internal representation of the STL string uses consecutive memory...modifying it the way described (cast away the 'const') basically introduces undefined behaviour to your application...it might work, it might crash, it might crash 1 minute later...Quote:
Originally posted by Yves M
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.
So...in order to do it safely...copy the returned constant character string to a normal character string which can be modified...after modification you simply assign it back to the STL string...
just wanted to be sure, is that ok?
string pszStr("Hello");
const char * pszCStr = pszStr.c_str();
char *pszStr= (char *)pszCStr;
can I modify pszStr?
ignore that looking backward again, I realize it's not ok
Why are you doing all this anyway? Just manipulate your "string" directly (or a copy of it as required). You need not (and normally should not) work with a C-style string unless you *have* to pass it to a legacy C-style function (one of your own hopefully). That doesn't seem to apply here so why are you troubling yourself?
Good point.
string s2(s1);
I realize s2 has a constructor, but I can't tell just by looking at definition (string.h)... where does it say?
Thanks!
I have to ask this because the reason i choose c# over c++ in a second is mainly because i hate c++ strings. Look how many questions get asked on just strings.
Why does the string class not give you back a c string that can be modified? and also while we are at why cant the string class convert between all string types? TCHAR, char, strings, wchar, and all of them. Maybe b strings i can see it cant switch between them but the rest are all doable so why not do it?