strings/knold-tot-c-like-1.cc - The Knold & Tot example with char*. | Lecture 3 - slide 12 : 27 Program 2 |
// Illustrates reference semantics of C-like strings. // Compiles with warnings, but does probably not execute correctly (likely: segmentation fault). #include <iostream> void g(){ char *s1 = "Knold", // Compiler Warnings: *s2 = "Tot"; // deprecated conversion from string constant to char* s1 = s2; // Both s1 and s2 points to the same "Tot" C-style string. s2[1] = 'u'; // Expected run-time error here, because literal strings in the program a non-mutable. std::cout << s1 << " og " << s2 << std::endl; } int main(){ g(); }