2.4.2. Pointers and const

Image

As with references, we can define pointers that point to either const or nonconst types. Like a reference to const, a pointer to const2.4.1, p. 61) may not be used to change the object to which the pointer points. We may store the address of a const object only in a pointer to const:

const double pi = 3.14;   // pi is const; its value may not be changeddouble *ptr = π        // error: ptr is a plain pointerconst double *cptr = π // ok: cptr may point to a double that is const*cptr = 42;               // error: cannot assign to *cptr

In § 2.3.2 (p. 52) we noted that there are two exceptions to the rule that ...

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.