Constant pointer and C++constant pointer

We have the following statement from the book by Stephen Prata-The C++ programming language (6th edition). Page 330:

You can assign the address of both constant and non-constant data to a pointer to a constant, assuming that this data is not itself a pointer, but you can only assign the address of non-constant data to a non-constant pointer.

In my understanding, the following things are available, which coincides with the first part of the opinion the author:

const int a = 5;
int b = 6;

const int * ptr = &a; //Допустимая операция, невозможно изменить значение a через указатель и переменную a.
const int * ptr2 = &b; //Допустимая операция, невозможно изменить значение b через указатель, но возможно через переменную b.

However, the part of the statement about "but to assign the address of non-constant data is allowed only to a non-constant pointer" causes a misunderstanding, according to the author's logic, the following action is unacceptable:

int a = 5;

int * const ptr = &a; //Недопустимо, так как указатель константный, указывает на не константный тип.

However, this code compiles successfully, which seems quite logical to me, since as a result we get a pointer without the possibility of changing the address, but nothing prevents us from changing the value of the variable through the pointer (*ptr).

The actual question is, what is the author trying to convey and what did I misunderstand from his statement?

Author: Vlad from Moscow, 2016-11-19

2 answers

The author meant the following in the second part of the statement.

If you have the following ads

int x;
const int cx;
int *p;

Then you can write

p = &x;

But you can't write

p = &cx;

The constancy of the pointer itself is not mentioned in this quote. It is simply an unfortunate translation that a pointer to non-constant data is called a non-constant pointer.:)

As for your example

int a = 5;

int * const ptr = &a; 

Then if you approach strictly, then there is no operation assignments. This is where the initialization of the constant object takes place. And if you actually use an assignment, the compiler will return an error message.

For example,

int a = 5;

int * const ptr;

ptr = &a;

This code snippet will not compile, because the constant pointer must be initialized at declaration, and, moreover, it cannot be assigned a value.

 3
Author: Vlad from Moscow, 2016-11-19 19:12:27

The author wanted to say this:

const int ci = 0;
int i = 0;

const int *cptr;
int *ptr;

int main(){
    cptr = &ci;     //ok
    cptr = &i;      //ok

    ptr = &ci;      //error: invalid conversion from 'const int*' to 'int*'
    ptr = &i;       //ok  
}
 1
Author: yrHeTateJlb, 2016-11-19 19:06:56