I
icecrime
arnuld said:On Mar 4, 12:12 pm, Ian Collins <[email protected]> wrote:arnuld wrote:Very very wrong!
char name[] = "hackers";
Is an automatic array of char, as such, it can be modified.
char *name = "hackers";
Is the incorrect way of writing
const char *name = "hackers";
A string literal should not be modified, to do so invokes undefined
behaviour and my cause your toilet to explode.
a "const char*" can easily be modified, see:
#include <iostream>
int main() {
int x = 7;
const char* author = "Stroustrup";
This means that the *pointee* is const, not the pointer.
std::cout << x
<< "\t"
<< author
<<"\n";
x = 100;
author = "Bjarne";
Here you're modifying the pointer, not the pointee. What the const is
keeping you from writing is for example:
author[0] = 'b'; // Illegal
However, is you had declared author as:
char * const author = "Stroustrup";
the pointer would've been const, but not the pointee :
author = "Bjarne"; // Illegal
author[0] = 'b'; // OK