From the chapter "A tutorial on Pointer" i see the following snippet of code
Code:
const int cintA = 10; // A constant int
int intB = 20; // A nonconstant int
const int *pointer_to_const_int = &cintA; // A pointer to a const int. The POINTER ITSELF IS NOT CONST.
*pointer_to_const_int = 20; // Compiler error *pointer_to_const_int is a constant
// and cannot be changed.
pointer_to_const_int++; // This is fine, it's simple pointer arithmetic
// moving the pointer to the next memory location.
// The pointer itself is not const so it may be repointed.
const int *const const_pointer_to_const_int = &cintA; // Here we have a constant pointer to a
// constant int. Notice as pointer is const we must
// initialise it here.
const_pointer_to_const_int++; // This is now illegal. The pointer itself is const
// and may not be repointed.
*const_pointer_to_const_int = 40; // Again compiler error. Pointer points to constant data.
// You cannot write through this pointer.
int *pointer_to_int = *intB;
*pointer_to_int = 40; // This is fine. Data pointed to is not constant.
pointer_to_int++; // Again fine, pointer is not constant, you may repoint it.
int *const const_pointer_to_int = &intB; // Constant pointer to non-constant int.
// Pointer is const so must be initialised here.
const_pointer_to_int++; // Compiler error. The pointer itself is constant and
// may not be repointed.
*const_pointer_to_int = 80; // This is fine. Although the pointer is constant the object
// pointed to is not constant and so may be written to.
Highlighted statment - Shouldn't that be?
Code:
int *pointer_to_int = &intB;
ssharish