A different class which does not do that? I don't understand..there will be cases where the string class would not copy the string? and why should it point?? it is an object not a pointer to a literal... ?
I will quicky google c_str(), no idea what it does..For #4 you need c_str() member function (will pass pointer to internal buffer, which you must not modify).
You ended that sentence with a preposition...Bastard!
lol you didn't answer my question.
well yeah true, the only thing i have done so far is convert binary string to floating pointer formats or
reverse a string that surely is modifying a string, safe because I am not adding or removing stuff. I am using that same string...
So I am just wondering "You should not modify a string literal..."
What does that statement mean?
You ended that sentence with a preposition...Bastard!
What kmdv is saying is that you could write another string class that does not perform such copying. Such a custom string object could contain a pointer that merely pointed to the string literal's first character. I do not recommend that you pursue this avenue of thought until you are far more skilled in understanding the basics of the std::string class and how to use it.Originally Posted by Eman
It means that you should not attempt to assign to any of the characters in a string literal, including the null terminator.Originally Posted by Eman
Last edited by laserlight; 12-27-2010 at 12:41 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
oh right...haha I doubt I could do even do what kmdv is thinking even if I wanted to :P
i tried this:
It compiled, I guess because I am using a C++ compiler so it didn't give any errors.Code:string str ="Hello" ; printf("%s", str)
But what it printed was utter nonsense..
I had to use
str.c_str()...which converts str to a c string? I didn't know printfs receive only pointers to strings....
Last edited by Eman; 12-27-2010 at 12:47 PM.
You ended that sentence with a preposition...Bastard!
yeah I know we can do that, but all this are modifying string literals..
and my lecturer
said "You should not modify string literals"
but in performing those operations, lower to upper, replace whitespace with a comma and all that.. it is still modifying a literal
haha i am gobsmacked, if it is illegal why not give a seg fault? lol
You ended that sentence with a preposition...Bastard!
The %s format specifier expects a corresponding argument that is a pointer to the first character of a null terminated string.Originally Posted by Eman
Because it results in undefined behaviour. One characteristic of undefined behaviour is that the code may appear to work perfectly fine until the time when you really need it to work (e.g., when demonstrating the program to the customer in front of the boss), upon which it inevitably fails, whether by crashing or by producing incorrect results.Originally Posted by Eman
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ok, maybe this will show why and what is illegal:
If your compiler merges duplicate constants it will print XOL BOB. The #1 and #2 operations are illegal and will not result in any seg fault probably, good if it does.Code:char* s1 = "LOL"; char s2[] = "BOB"; "LOL"[0] = 'X'; // #1 "BOB"[0] = 'X'; // #2 std::cout << s1 << " " << s2 << endl;
String underlying s1 is a literal, string underlying s2 is NOT (it's a copy).
Huh.
Last edited by kmdv; 12-27-2010 at 12:51 PM.
Last edited by Eman; 12-27-2010 at 12:54 PM.
You ended that sentence with a preposition...Bastard!
Maybe this will workCode:char* s1 = "LOL"; char s2[] = "BOB"; const_cast<char*>("LOL")[0] = 'X'; // #1 const_cast<char*>("BOB")[0] = 'X'; // #2 std::cout << s1 << " " << s2 << endl;
yeah that worked and gave me an error.
so you are converting a ("LOL")[0] to be a pointer to the string "LOL" to 'L' and you try to write to it by replacing X with the first element 'L' ?Code:const_cast<char*>("LOL")[0] = 'X'; // #1
did your compiler give you no error? maybe i should use borland.
But you are copying a literal into a character array. so s2 should be a string literalstring underlying s2 is a copy....not a literal.
yes that happened to me when I was giving my assignment demo I need to learn to use a debugger!Because it results in undefined behaviour. One characteristic of undefined behaviour is that the code may appear to work perfectly fine until the time when you really need it to work (e.g., when demonstrating the program to the customer in front of the boss), upon which it inevitably fails, whether by crashing or by producing incorrect results.
Last edited by Eman; 12-27-2010 at 01:01 PM.
You ended that sentence with a preposition...Bastard!
What error? Borland has merging off by default. Try this:
It copies its contents, not a pointer or something. You get each character from the literal and put it into a buffer. You only read from literal, any write is performed to the buffer not literal.Code:char* s1 = "LOL"; char s2[] = "BOB"; char* lol = "LOL"; char* bob = "BOB"; lol[0] = 'X'; // #1 bob[0] = 'X'; // #2 std::cout << s1 << " " << s2 << endl;
The C++ standard states that attempting to modify a string literal results in undefined behaviour. Attempting to modify a string literal means attempting to assign to any of its characters, including the null character. What else do you not understand?
Trying to come up with code that demonstrates why this rule exists has some merit, but the problem is that this is implementation dependent. The main point is that the contents of a string literal may be stored in a location that is read-only. kmdv's most recent point is that as an optimisation, repeated instances of the same constant may be reduced to a single instance.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
the code compiled perfectly, both of them. But on runtime it crashed.
"Unhandled exception, Access violation.."
But I don't think that is the outcome you want, because you want to prove to me that a write to a string literal will not always give me a seg fault..
if Borland doesnt work should I try Dev C++ then? the last of the compiler :O haha
So a pointer to a string is a literal. But an array of characters or a string object is not a literal.It copies its contents, not a pointer or something. You get each character from the literal and put it into a buffer. You only read from literal, any write is performed to the buffer not literal.
You ended that sentence with a preposition...Bastard!