Before discussing pointers, you need to know how values assigned to variables are stored in the memory of the computer. In C++ programing, a variable is used to store data (value). A variable may store an integer value, a float value, a string value or a character and so on. When you declare a variable, memory is kept aside (allocated) for that variable. The size of the memory kept aside, depends on the data type of the variable. You can think of the memory of the computer, as a succession of memory cells, where each cell has a unique address.
Let’s look at an example:
int x =5;
In the above example, the value “5” is stored in the memory allocated to the variable “x”. A diagrammatic representation of the above example is as follows:
You use the name of the variable to access the contents (value) it stores. The following cout statement, displays the value ”5” on the screen:
cout<<x<<endl;
What if you need to know the exact location in the computer’s memory that has been allocated to the variable? For this you need to know the memory address of the variable. To store the memory address of variables, you use pointers.
In essence, you can say that a pointer is variable that stores the memory address of another variable. Before we discuss pointers in detail, you need to know about two operators – Reference Operator and Dereference Operator.
A pointer stores the memory address of another variable as its value. To be able to store the memory address of a variable, you must know how to retrieve it. The reference operator also known as the address operator, denoted by the “&” sign, is used to retrieve the memory address of a variable. Let’s say you have a variable “age”, and you initialize it to the value 34. The C++ statements to do this are as follows:
int age;
age=34;
To know where in the memory is the variable “age” located, you need to know the memory address of the variable “age”. To obtain the memory address, the reference operator should precede the variable name as follows:
&age
A diagrammatic representation of the above example is as follows:
Let’s look at the following cout statements:
- cout<<age<<endl;
- cout<<&age<<endl;
The first cout statement displays the value “34”, whereas the second displays the memory address of the variable “age”, which based on the above diagram is 1113.
Thus, the reference operator helps you retrieve the memory address of a variable. The name of the variable is used to access the value (content) it stores.
12.2 Dereference Operator (*)
Till now you have used the name of the variable to access the value it stores. You can also access the value of a variable using a pointer. The Dereference Operator or the Contents of Operator allows you to obtain the content stored at a memory location which the pointer points to. To do so, you need to precede the name of the pointer variable with the “*” symbol. The syntax is as follows:
*pointervariablename
Let’s look at an example:
- int age=34;
- int *ageptr;
- int *ageptr = &age;
- In the first statement, you declare the variable “age” and initialize it to 34.
- In the second statement, you declare a pointer variable “ageptr”
- In the third statement, you initialize the pointer “ageptr ” to store the memory address of the variable “age”
Logical representation of the above example in the memory is as follows:
The variable “age” is stored at the memory location 0003 , and the value assigned to it is 34.
You can indirectly access the value of the variable age by using the Derefence operator.
Both the following statements display “34” on the screen:
cout<<age<<endl;
cout<<*ageptr<<endl;
The following statement is also true:
age=*ageptr
In essence, “ageptr” refers to the memory location 0003 and *ageptr refers to the value pointed by “ageptr”. In other words, a variable referenced with “&” can be dereferenced with “*”.
12.3 Using Pointer as an Alias of Variable
You can use a pointer variable as an alias of the variable it points to.
Let’s see how this works:
- age= age+6
The above statement obtains the value of the variable “age”, adds “6” to it and assigns the result to variable “age”:
The first statement can also be written as follows:
- *ageptr=*ageptr+6
In the above statement you have not used the variable name ”age” to access the value it stores, but indirectly accessed the value by derefecing the pointer. The pointer “ageptr” points to the variable “age”. In the above statement ”*ageptr” obtains the value assigned to the variable “age”, add “6” to it, and stores the result at the memory location assigned to the variable “age”.
12.4 Declaring Pointers
A pointer, as you know, is variable that stores the address of another variable. Declaring pointers is similar to declaring other variables; you need a data type and a name. In addition, the unary operator “*” must precede the variable name. When the “*” operator precedes a variable name, it tells the compiler that the variable is not a regular variable but a pointer variable.
The syntax for declaring pointers is as follows:
Data type *variable name
- Data type of a pointer variable is the same as the data type of the variable whose memory address it stores.
- “*” helps distinguish between a pointer variable and a normal variable. A pointer variable can only store a memory address as its value.
Let’s look at an example declaration:
int *ptr
- “*” tells the complier that the variable “ptr” is a pointer variable
- The data type of “ptr” is integer so it can store the memory address of any integer variable.
Let’s look at another example:
- int age;
- age= 34;
- int * ageptr;
- ageptr =&age;
Understanding the statements of the above example:
- In the first statement, you declare an integer variable “age”
- In the second statement, you assign the variable “age” the value 34
- In the third statement, you declare a pointer variable “ageptr”
- In the fourth statement, you assign the memory address of the variable “age” to the pointer variable “ageptr”
Let’s look at a logical representation of the above example:
- The variable age is an integer variable that stores the value 34. It is allocated the memory location 1113
- The variable ageptr is a pointer variable and stores the memory address of the variable age, which is 1113.
Let’s look at the following cout statements:
- cout<<age<<endl;
- cout<<&age<<endl;
- cout<<ageptr<<endl;
- cout<<*ageptr<<endl;
- The first statement displays the value of the variable “age”, which is 34
- The second statement displays the memory address of the variable “age”, which is 1113
- The third displays statement the value assigned to the pointer variable “ageptr”, which is 1113
- The fourth statement display the value stored at the memory location 1113, which is 34
12.5 Pointer Initialization
A pointer can store only a memory address. So when you initialize a pointer, you specify the memory address it’s going to point to, and not the value stored at that memory address.
Let’s look at example:
- int x;
- int *xptr;
- xptr=&x;
- In the first statement, you declared a variable “x”. The complier sets aside memory for this variable (based on its data type)
- In the second statement, you declare a pointer variable “xptr”
- In the last statement, you initialize the pointer variable “xptr” by assigning it the memory address of the variable “x”
A logical representation of the above example in the memory is as follows:
The pointer variable “xptr” points to the memory address of the variable “x”. In the above table, the variable “x” doesn’t have any value associated with it because we haven’t assigned the variable any value.