c++ - Explain the following about pointers and const with example -
"you can assign address of either const data or non-const data pointer-to-const, provided data type not pointer, can assign address of non-const non-const pointer."
reference: c++ primer plus stephen prata
if have non-const object, say
int = 10; then can assign address pointers both const , non-const ints.
int* p = &a; //ok const int* pc = &c; //ok can't modify *pc if, on other hand, have const object, can assign address pointer const
const int = 10; int* p = &a; //error, because allow modifying `a` via `*p` const int& pc; //ok one problem have provided quote uses term "const pointer" refer known "pointer const". compare
int * p const = nullptr; this const pointer. cannot change value of pointer itself, i.e. points to.
const int * p; although book refers const pointer, pointer not const. can things p = &a; p = nullptr; etc.
Comments
Post a Comment