(C++) const char* to char* - how?
Some functions or methods only return constant strings. But I want to be able to modify these strings as well. How could I do that?
Thanks.
Quote:Some functions or methods only return constant strings. But I want to be able to modify these strings as well. How could I do that?Create a mutable copy of the returned string, and work with the copy:
std::string text = function_that_returns_a_const_char_ptr();// Do stuff with 'text'...
Quote:Original post by asdqwe
Some functions or methods only return constant strings. But I want to be able to modify these strings as well. How could I do that?
Thanks.
If a function returns a 'const char*', then that function is saying "here's a pointer to some memory with characters in it, but please don't modify this memory because it belongs to me". In other words, it's a bad idea to go modifying it.
For educational purposes though, the code to do this is:
const char* myConstCharPointer = ...;
char* badAndWrong = const_cast<char*>( myConstCharPointer );
Instead of doing the above, you should probably make a copy of the string and modify your own copy:
std::string newCopy = myConstCharPointer;
Or if you want to do it in C instead of C++:
char* newCopy = malloc( sizeof(char) * (strlen(myConstCharPointer)+1) );
strcpy( newCopy, myConstCharPointer );
//do stuff
free( newCopy );
Quote:Original post by Hodgmanconst char* myConstCharPointer = ...;char* badAndWrong = const_cast<char*>( myConstCharPointer );
Which is only legal if the pointee wasnt const to begin with.
const char* p = "Hello World!"; // unmodifiable string literalchar* q = new char[strlen(p) + 1];strcpy(q, p);const char* x = q;char* y = const_cast<char*>(x); // legal, because *y isn't actually constchar* z = const_cast<char*>(p); // illegal, because *z is actually const
Quote:Original post by Hodgman
Or if you want to do it in C instead of C++:
If you wanted to do it in C, you'd just use strdup().
Quote:Original post by DevFred
*** Source Snippet Removed ***
Unfortunately, one of the stupidinteresting aspects of the C++ language inherited from C is that string literals can be assigned to non-const char pointers. Which means that a string literal is not necessarily unmodifiable. The language would be certainly cleaner if the were.
Quote:Original post by DevFredQuote:Original post by Hodgmanconst char* myConstCharPointer = ...;char* badAndWrong = const_cast<char*>( myConstCharPointer );
Which is only legal if the pointee wasnt const to begin with.
As I understand it, as long as you don't modify the string, it's also legal to cast a const char * to a char * using const_cast<>.
According to "The C++ Programming Language", that's the exact reason this cast was introduced at all, to allow for compatibility with legacy libraries and code that isn't const correct (or const aware at all).
The only action that should lead to undefined behavior is const_cast<>ing a const away and modifying the pointee, regardless of whether the original variable was declared as const or not.
Quote:Original post by SiCraneThere is still hope, though. This conversion has been deprecated for 10 years (Annex D.4 of ISO C++98), and indeed gcc is complaining loudly about it now, if -Wall is enabled.
Unfortunately, one of the stupidinteresting aspects of the C++ language inherited from C is that string literals can be assigned to non-const char pointers. Which means that a string literal is not necessarily unmodifiable. The language would be certainly cleaner if the were.
People considered the warning a "nuisance" in the past, which is why it was never enabled by default. It looks like the wind is turning, though :-)
With any luck, C++1x or C++2x comes out one day and cleans up with those deprecations for good. At least, such are my hopes.
Quote:Original post by SiCrane
Which means that a string literal is not necessarily unmodifiable.
The compiler will let you write that code, yes. But its behavior is undefined. Hm, are we saying the same thing?
A philosophical question:
char * foo();
Let's say this function returns "Hello World\0".
Are you allowed to write "Hello World !\0" into that string?
Or more directly: how do you know how many chars you are allowed to write into such string, if you didn't allocate it?
In case of Hello World, is it safe to assume that you may write 11 characters + \0?
What if the string returned is a pointer into larger array:"ABC\0Hello World\0Hi there\0", so that return value is array[4]? And what if you replace the result with "Hello\0", thereby completely destroying the original structure into "ABC\0Hello\0World\0Hi there\0"?
Needless to say - char * strings have too many pitfalls, so this approach is essentially forbidden. This is why all such APIs do not return strings, but pass them into function by caller.
char * foo();
Let's say this function returns "Hello World\0".
Are you allowed to write "Hello World !\0" into that string?
Or more directly: how do you know how many chars you are allowed to write into such string, if you didn't allocate it?
In case of Hello World, is it safe to assume that you may write 11 characters + \0?
What if the string returned is a pointer into larger array:"ABC\0Hello World\0Hi there\0", so that return value is array[4]? And what if you replace the result with "Hello\0", thereby completely destroying the original structure into "ABC\0Hello\0World\0Hi there\0"?
Needless to say - char * strings have too many pitfalls, so this approach is essentially forbidden. This is why all such APIs do not return strings, but pass them into function by caller.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement