New to C++. Question about constant pointers

c++, constants, pointers

Solution

A simple way to remember how const works with pointers is to remember that it always applies to whatever is to the left of it, unless it's the left-most keyword, in which case it applies to the right.

Examples:

Pointer to a constant char: The pointer can be changed to point to something else, but the char it initally points to cannot change value.

const char * p;

Constant pointer to a char: The pointer cannot be changed to point to anything else, but the char it points to can change value.

char *const p;

Constant pointer to a constant char: The pointer cannot be changed to point to anything else, and the char it points to cannot change value.

const char *const p;

Problem

I am trying to learn C++ via some web tutorials. I don't have a compiler available to me, otherwise I would try this out. I'm not sure what is meant by a const pointer. Does that just mean it always points to the same memory address? Why would you ever want to do that? Would the following code be legal? ``` ... int * const aPointer = new int; ... //do something with aPointer delete aPointer; ... //do something else, including possibly more 'new' statements aPointer = new int; ... ```

Original source

Related problems