-
October 1st, 2003, 08:16 AM
#1
casting from std:string to char *
How do I cast from std:string to char *?
Thanks
avi123
-
October 1st, 2003, 08:20 AM
#2
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()
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
October 1st, 2003, 08:31 AM
#3
can I convert const char* to char* ?
-
October 1st, 2003, 08:38 AM
#4
I didn't mean the last question seriously..
-
October 1st, 2003, 08:39 AM
#5
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 *"?
-
October 1st, 2003, 08:46 AM
#6
Originally posted by avi123
can I convert const char* to 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.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
October 1st, 2003, 09:41 AM
#7
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?
-
October 1st, 2003, 09:43 AM
#8
Don't change the size of the string.
-
October 1st, 2003, 09:45 AM
#9
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
-
October 1st, 2003, 09:47 AM
#10
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?
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.
If you want to modify it, you'll have to make a non-const copy of the string.
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;
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.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
October 1st, 2003, 09:52 AM
#11
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
That's a bug in your compiler. It should not allow changing a const object.
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):
Code:
string s = "Hello";
const char *sz = s.c_str();
sz[0] = 'B';
And the error is pretty descriptive:
Error: l-value specifies const object
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
October 1st, 2003, 10:00 AM
#12
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
-
October 1st, 2003, 10:16 AM
#13
Is this what you mean?
Code:
char *s1 = "123456";
string s2 = s1;
-
October 1st, 2003, 10:22 AM
#14
yep
is that also ok?
char *s1 = "123456";
string s2(s1);
-
October 1st, 2003, 10:31 AM
#15
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|