Pointer in C++
Introduction to pointer
- Pointers are variables used to store the address of another variable.
- It is an extremely powerful programming tool.
- It helps to improve program's efficiency and allows to handle unlimited amount of data.
Syntax of Pointer declaration:
datatype * ptr_name;Address of operator (&)
- Address of operator is called as the Referencing operator.
- The ampersand (&) is the referencing operator that gives the address of that variable.
- It returns the address of the variable associated with the operator.
- Address is displayed in hexadecimal form.
- Address of variable is the memory location number alloted to the variable.
Example:
int *p , a;
a=10;
p=&a;
- In the above example, &a returns the address of the variable a. p is a pointer which points to a variable a.
- p=&a means "copy the address of the variable a in the pointer variable p".
Value of operator (*)
- Value of operator is called as the De-referencing operator.
- It returns the value stored in the variable pointed by the specified pointer.
- The asterisk (*) is a dereference operator which means "value pointed to by".
- In the above example, the *p will return the value of the variable pointed by the pointer p.
- a=*p means that the value of the variable pointed by the pointer p is stored in the variable a.
Example : Demonstrating the referencing & de-referencing using pointers
#include <iostream>
using namespace std;
int main()
{
int *p, a;
a = 10;
cout<<"Address of a (&a): "<<&a<<endl<<endl;
cout<<"Value of a (a): "<<a<<endl<<endl;
p = &a; // Pointer p holds the memory address of variable a
cout<<"Address that pointer p holds (p): "<<p<<endl<<endl;
cout<<"Content of the address pointer p holds (*p): "<<*p<<endl;
a = 20; //The content inside memory address &a is changed from 10 to 20
cout <<"Address pointer p holds (p): "<<p<<endl<<endl;
cout <<"Content of the address pointer p holds (*p): "<< *p << endl;
*p = 5;
cout<<"Address of a (&a): "<<&a<<endl<<endl;
cout<<"Value of a (a): "<<a<<endl<<endl;
return 0;
}
Output:
Address of a (&a): 0x7ffc99a8e9a4
Value of a (a): 10
Address that pointer p holds (p): 0x7ffc99a8e9a4
Content of the address pointer p holds (*p): 10
Address pointer p holds (p): 0x7ffc99a8e9a4
Content of the address pointer p holds (*p): 20
Address of a (&a): 0x7ffc99a8e9a4
Value of a (a): 5
- In the above program, when a=10; the value 10 is stored in the address of variable a.
- When p=&a; the pointer p holds the address of a and the expression *p contains the value of that address which is 10;
- When a=20; the address that pointer p holds is unchanged, but the expression *p is changed because the address &a (which is same as p) contains 20.
- When *p=5; the content in the address p which is equal to &a is changed from 20 to 5. Since the pointer p and variable a has address, value of a is changed to 5.
- 0x in the beginning represents the address is in hexadecimal form.
Note: You may not get the same address as an output on your system.