Const Pointer

◀ Check Value Inputted by User▶ Input Output Redirection
Amazon Pointers are already confusing, but they could get even more complicated when they are used in conjunction with const. This section is intended to clear you of any doubt you may have regarding manipulating const pointers. Let’s examine each case with examples.

Case 1: a const pointer pt that points to an int
int apple = 10;
int * const pt = &apple;

This declaration says that pt can point to only apple and nothing else. Therefore, you cannot make it point to another address later in the program. However, you can modify the value of apple through *pt or apple.

Case 2: a pointer pt that points to a const int
int apple = 10;
const int * pt = &apple;

This declaration states that pt points to a const int; so you cannot use pt to modify the value of apple. However, apple is not const, so you can change the value of apple by assigning a new value to apple. Also, pt can point to another address later.

Case 3: a pointer that points to an int
int apple = 10;
int * pt = &apple;

This declaration is what we normally use. You can change the value of apple through apple and *pt, and you can make pt point to another address later.

Case 4: a const pointer that points to an int, which is declared to be const
const int apple = 10;
int * const pt = &apple;

You should know from the first case that pt cannot point to another variable due to its const status. However, what about modifying the value of apple? If you think about it a little, you may figure out something incoherent is going on here.

If apple is made const, its value is not supposed to change no matter what. However, it seems as if you can use pt to change apple’s value.

Due to this discrepancy in the meaning of const, C++ forbids such assignments.
Case 5: a pointer that points to const int, which is also declared to be const
const int apple = 10;
const int * pt = &apple;

In this case, you cannot change the value of apple via *pt or apple. However, pt can still point to another variable later if you want it to.

Case 6: a pointer that points to int, which is declared to be const
const int apple = 10;
int * pt = &apple;

Again, if you think about it a little, you may figure out something incoherent is going on in these declarations. If apple is made const, its value is not supposed to change no matter what. However, it seems as if you can use pt to change apple’s value.

Due to this discrepancy in the meaning of const, C++ forbids these assignments.


Having learned how to use const pointers, you should understand the difference between placing const before and after the type name. Consider the following declarations:
int apple = 10;
const int * const pt = &apple;
You should know immediately that pt cannot be used to modify the value of apple and cannot point to something else either. In this case, apple can be used to change its own value. Consider the following:
const char * const color[8] =
{
	"blue", "green", "red", "white", "black", "purple", "pink", "gray"
};
In this case, no strings can be changed and no pointers in the array can be made pointing to something else.

Next let’s look at how to properly do file input redirection or output redirection!
◀ Check Value Inputted by User▶ Input Output Redirection

fShare
Questions? Let me know!