The const
keyword frequently appears in the declaration of pointer variables. It is used to restrict what can be done with the pointer.
Things you can ordinarily do with a pointer
Let's say we have the following array declaration and pointer declaration:
int scores[6] = {100, 95, 87, 63, 78, 89}; int num = 10; int* p = #
(An array name is converted into a pointer to the first element of the array when used in a value context, so most of the things you can do with pointers can also be done with an array name.)
Here's a list of things we can do with the pointer we've declared (and - in some cases - with the array name):
Dereference the pointer to get the value it points to. Examples:
cout << *p << endl; // Prints 10 cout << *scores << endl; // Prints 100
Subscript the pointer to get the value it points to. Examples:
cout << p[0] << endl; // Prints 10 cout << scores[2] << endl; // Prints 87
Use pointer arithmetic notation and the dereference operator with the pointer to get the value it points to. Examples:
cout << *(p + 0) << endl; // Prints 10 cout << *(scores + 2) << endl; // Prints 87
Dereference the pointer to change the value it points to. Examples:
*p = 5; // Changes num to 5 *scores = 5; // Changes scores[0] to 5
Subscript the pointer to change the value it points to. Examples:
p[0] = 5; // Changes num to 5 scores[2] = 5; // Changes scores[2] to 5
Use pointer arithmetic notation and the dereference operator with the pointer to change the value it points to. Examples:
*(p + 0) = 5; // Changes num to 5 *(scores + 2) = 5; // Changes scores[2] to 5
Change the adddress stored in the pointer (this does not work with an array name). When you add to or subtract from the address stored in a pointer variable, the arithmetic is scaled using the size of the data type the pointer points to. For example, adding 4 to a pointer to an int
actually adds 16 to the address stored in the pointer (4 * sizeof(int)
= 4 * 4 = 16), while adding 4 to a pointer to a char
adds only 4 to the address stored in the pointer (4 * sizeof(char)
= 4 * 1 = 4). Examples:
p++; // Adds 4 to the address in p p = p + 2; // Adds 8 to the address in p p -= 3; // Subtracts 12 from the address in p
The const
keyword can be used in the declaration of a pointer variable to restrict what can be done with the pointer. There are three possible variations:
Pointer to constant data This prevents you from changing the value of the variable that the pointer points to.
Example:
char const* p const char* p
Allows:
#1 through #3 and #7 from the list above.
Does not allow:
#4 through #6 from the list above.
Constant pointer to non-constant data This prevents you from changing the address stored in the pointer variable.
Example:
char* const p
Allows:
#1 through #6 from the list above.
Does not allow:
#7 from the list above.
Constant pointer to constant data This prevents you from changing the value of the variable that the pointer points to and also prevents you from changing the address stored in the pointer variable.
Example:
const char* const p char const* const p
Allows:
#1 through #3 from the list above.
Does not allow:
#4 through #7 from the list above.
const
KeywordThe const
keyword also frequently appears in the declaration of reference variables. It is used to restrict what can be done with the reference.
Things you can ordinarily do with a reference
Let's say we have the following reference variable declaration:
string greeting = "hello"; string& s = greeting;
Here's a list of things we can do with the reference variable we've declared (this is a much shorter list than what we can do with a pointer):
Use the reference variable to get the value of the variable it refers to. Example:
cout << s << endl; // Prints "hello"
Use the reference variable to change the value of the variable it refers to. Example:
s = "goodbye"; // Changes greeting to "goodbye"
The const
keyword can be used in the declaration of a reference variable to restrict what can be done with it. There's only one possible use:
Reference to constant data This prevents you from changing the value of the variable that the reference variable refers to.
Example:
const string& s const const& s
Allows:
#1 from the list above.
Does not allow:
#2 from the list above.
There is no such thing as a "constant reference." Reference variables are effectively always constant, since once you've initialized a reference variable there's no way to make it refer to a different variable instead.