Please explain me in detail :

const char *str1 = "pointer to constant";
char *const str2 = "constant pointer";
const char *const str3 = "constant pointer to constant";
str1[0] = 'P'; // illegal!
str1 = "ptr to const"; // ok
str2 = "const ptr"; // illegal!
str2[0] = 'P'; // ok
str3 = "const to const ptr"; // illegal!
str3[0] = 'C'; // illegal!

Why? Doesn't your book do a good job? Or Google?

>>const char *str1 = "pointer to constant"

That means that the pointer points to a const data. Which means that
the pointer can't change the const data that its pointing since a const
data cant be changed. But it can point to somewhere else.

>>char *const str2 = "constant pointer";

That means the pointer is const and cannot change what its pointing to.
But it can change what its pointing to.

>>const char *const str3 = "constant pointer to constant";
That is a combination of the 2 stated before. It is a const pointer which
points to a const data. That is, it cannot change the value its pointing to
nor can it change the address that its pointing to.

The rest of the code is just showing the above statements.

const char *str1 = "pointer to constant";

str1 refers to a char which is constant(Read only)
so you get this not working.

str1[0] = 'P'; // illegal!
char *const str2 = "constant pointer";

str2 refers to a constant pointer to a char, here address is read only!
So this works:

str2[0] = 'P'; // ok

str3 is a constant pointer to a constant character, both address and value are read only.

An aid to memory is the immediate right hand side to 'const' is read only.

Hope this was helpful.
cheers

Please explain me in detail :

const char *str1 = "pointer to constant";
char *const str2 = "constant pointer";
const char *const str3 = "constant pointer to constant";
str1[0] = 'P'; // illegal!
str1 = "ptr to const"; // ok
str2 = "const ptr"; // illegal!
str2[0] = 'P'; // ok
str3 = "const to const ptr"; // illegal!
str3[0] = 'C'; // illegal!

Frankly, to me what you posted was a detailed explanation. :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.