OOPortal OOPortal
Pointers/Memory Allocation   «Prev 

Pointer Constant Example

The statement
const int* const cp;

declares cp to be a constant pointer whose pointed-at value is also constant.
void fcn(const int* const p){
   // within here it is illegal
   // to have  *p = expression;
   // also illegal is p = expression; 
}

Constant Pointers and Pointers to Constants

const char* pstars[] {
"Fatty Arbuckle", "Clara Bow",
"Lassie", "Slim Pickens",
"Boris Karloff", "Mae West",
"Oliver Hardy", "Greta Garbo"
};

Here you are specifying that the objects pointed to by elements of the array are constant. The compiler inhibits any direct attempt to change these, so an assignment statement such as this would be flagged as an error by the compiler, thus preventing a nasty problem at runtime:
*pstars[0] = 'X'; // Will not compile...

However, you could still legally write the next statement, which would copy the address stored in the element on the right of the assignment operator to the element on the left:
pstars[5] = pstars[6]; // OK

Those lucky individuals due to be awarded Ms. West would now get Mr. Hardy, because both pointers now point to the same name. Of course, this has not changed the object pointed to by the sixth array element, it has only changed the address stored in it, so the const specification has not been breached. You really ought to be able to inhibit this kind of change as well, because some people may reckon that good old Ollie may not have quite the same sex appeal as Mae, and of course you can.
Look at this statement:
const char* const pstars[] {
 "Fatty Arbuckle", "Clara Bow",
 "Lassie", "Slim Pickens",
 "Boris Karloff", "Mae West",
 "Oliver Hardy", "Greta Garbo"
};