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.
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.
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?
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.
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.
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.
Bookmarks