In a C++ programming language, variable is used to hole the value & this variable stored in a memory.
Each variable has a address to identify where these variables actually stored in a memory.
In a C++ programming language, pointer is a variable which can hold the address of another variable.
Accessing Address of Variable
In a system every memory location has its address & we can access the address using ampersand (&) operator.
This ampersand (&) operator denotes an address in memory.
Ex: int i = 20;
char c = ‘a’;
In the above example, there are two variable i & c.
Integer variable “i” stores the value 10 at memory location 1024(for example) and character variable “c” stores the value ‘a’ at memory location 1026.
cout<<“\n value of variable i= “<< i ;
cout<<“\n value of variable c= “<< c ;
Output
value of variable i= 20
value of variable c= a
If we want to print the address of variable ‘i and c’ then we need ampersand (&) operator .
cout<<“\n Address of variable i = “<< &i ;
cout<<“\n Address of variable c= “<< &c ;
Output: Address of variable i = 1024
Address of variable c= 1026
Note: When you run this program, address are printed might be different.Example: Write a program to print the address of the variable.
#include1 2 3 4 5 6 7 8 9 10 11 12 | int main() { int v1; int v2; float v3; cout << "\n Address of variable v1= " << & v1; cout << "\n Address of variable v2= " << & v2; cout << "\n Address of variable v3= " << & v3; return 0; } |
Output
1 2 3 | Address of variable v1= 0x7ffdd72d170c Address of variable v2= 0x7ffdd72d1710 Address of variable v3= 0x7ffdd72d1714 |
Note: Each and every time when you run this program these variables stored in a different memory location.
Pointer Variable
pointer variable is hold the address of another variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <iostream> using namespace std; int main() { int i = 20; // declare & initialize variable i int * ptr; // declare a pointer variable ptr ptr = & i; // ptr (pointer variable) holds the address of variable i. cout << "\n Address of variable i = " << & i; cout << "\n Address of variable i = " << ptr; return 0; } |
Output
1 2 | Address of variable i = 0x7fff9a23444c Address of variable i = 0x7fff9a23444c |
Value at address
In the above example, there are two variable, first is normal variable ‘i’ & pointer variable ‘ptr’.
Integer variable “i” stores the value 10 at memory location 1024(for example) and pointer variable “ptr” stores the address of variable ‘i’ at memory location 3045.
Example:
#include1 2 3 4 5 6 7 8 9 10 11 | using namespace std; int main() { int i = 10; // declare & initialize variable i int * ptr; // declare a pointer variable ptr ptr = & i; // ptr (pointer variable) holds the address of variable i. cout << "\n value of variable i= " << * ptr; return 0; } |
Output:
value of variable i= 10
Note: Pointer variable “ptr” prints the address of variable and “*ptr” prints the value of i. *variable name (*ptr) is also called value at address.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> using namespace std; int main() { int a = 4; int * b; b = & a; cout << "Address of a = " << & a; cout << "\n Address of a = " << b; cout << "\n Address of b = " << & b; cout << "\n Value of b =" << b; cout << "\n Value of a = " << a; cout << "\n Value of a = " << * ( & a); cout << "\n Value of a = " << * b; return 0; } |
Output
1 2 3 4 5 6 7 | Address of a = 2464 Address of a = 2464 Address of b = 2466 Value of b = 2464 Value of a = 4 Value of a = 4 Value of a = 4 |
Note: When you run this program, address are printed might be different.
Pointer to Pointer
In a C language, a pointer is an address. Normally, a pointer variable contains the address of a another variable.
A pointer to a pointer is a chain of pointers.
Or
Pointer to pointer means one pointer variable holds the address of another pointer variable.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <iostream> using namespace std; int main() { int a = 4; int * b, ** c; b = & a; c = & b; cout << "Address of a = " << & a; cout << "\n Address of a = " << b; cout << "\n Address of a = " << * c; cout << "\n Address of b = " << & b; cout << "\n Address of b = " << c; cout << "\n Address of c =" << & c; cout << "\n Value of c = " << c; cout << "\n Value of c = " << * ( & c); cout << "\n Value of b = " << b; cout << "\n Value of b = " << * ( & b); cout << "\n Value of b = " << * c; cout << "\n Value of a = " << a; cout << "\n Value of a = " << * ( & a); cout << "\n Value of a = " << * b; cout << "\n Value of a = " << ** c; return 0; } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Address of a = 2464 Address of a = 2464 Address of a = 2464 Address of b = 2466 Address of b = 2466 Address of c = 2468 Value of c = 2466 Value of c = 2466 Value of b = 2464 Value of b = 2464 Value of b = 2464 Value of a = 4 Value of a = 4 Value of a = 4 Value of a = 4 |
NULL Pointers
A pointer that is assigned NULL is called a null pointer. We must initialize NULL pointer during variable declaration.
A null pointer refers to pointer variable that does not point to a valid address.
Generally NULL pointer is used when you do not have an exact address to be assigned.
1 2 3 4 5 6 7 8 9 10 11 | #include <iostream> using namespace std; int main() { int * ptr = NULL; cout << "The value of ptr is : " << ptr; return 0; } |
Output
1 | The value of ptr is : 0 |