Pointers in C++ programming
POINTER: Pointer is a variable, which stores address of another variable.
Pointer notation uses two symbols:
- * (Asterisk) symbol. * is called ‘value at address’ operator.
- & (Ampersand) symbol. & is called ‘address of’ operator
Pointer = Address Number
= Memory Location |
Note that pointer is always an integer value. It is just like a student roll number, which is always an integer value. Note that roll number cannot be represented using float value.
POINTER NOTATION
Consider the declaration, int i = 3;
This declaration tells the Turbo C++ compiler to:
- Reserve space in memory to hold the integer value.
- Associate the name i with this memory location.
- Store the value 3 at this location
We may represent i’s location in memory by the following memory map.
Assume that computer has selected memory location 65524 as the place to store the value 3. The location number 65524 is not a number to be relied upon, because some other time the computer may choose a different location for storing the value 3. The important point is, i’s address in memory is a number.
We can print this address number through the following program:
#include<iostream> using namespace std; int main( ) { int i = 3 ; cout<<"Address of i = "<<&i; cout<<endl; cout<<"Value of i = "<<i; } The output of the above program would be: Address of i = 65524 Value of i = 3 The other pointer operator available in C++ is ‘*’, called ‘value at address’ operator. It gives the value stored at a particular address. The ‘value at address’ operator is also called ‘indirection’ operator. Observe carefully the output of the following program: #include<iostream> using namespace std; int main( ) { int i = 3 ; cout<<"Address of i = "<<&i; cout<<endl; cout<<"Value of i = "<<i; cout<<endl; cout<<"Value of i = "<<*(&i);
}
The output of the above program would be:
Address of i = 65524
Value of i = 3
Value of i = 3
Note that printing the value of *( &i ) is same as printing the value of i.
The expression &i gives the address of the variable i. This address can be collected in a variable, by saying, j = &i;
But remember that j is not an ordinary variable like any other integer variable. It is a variable that contains the address of other variable (i in this case). Since j is a variable the compiler must provide it space in the memory. Once again, the following memory map would illustrate the contents of i and j.
POINTER TYPES:
Integer pointer: Integer pointer means that the pointer stores address of an integer value. Syntax of declaring int pointers: int x = 10; // x is an integer variable, which stores value 10 int *ptr; // ptr is a pointer variable ptr = &x; // ptr stores address of variable x int *p1, *p2, *p3; //declares three integer pointers p1, p2, p3, but are not assigned anything
Example of integer pointer:
#include<iostream> using namespace std; int main( ) { int x = 30; // declaring normal variable x and assigning 30 to it int *Mars; // declaring pointer, whose name is Mars Mars = &x; // now hosanna points to variable x or stores address of variable x cout<<x; // printing value of x cout<<Mars; // printing value of pointer Mars cout<<*Mars; // prints value at address location Mars cout<<&x; // same as printing value of pointer cout<<*(&x); //same as printing value of x, which is 10 }
Float pointer:
Float pointer means that the pointer stores address of float value.
Syntax of declaring float pointers:
float x = 25.0 // x is an float variable, which stores value 25.0 float *p; // p is a pointer variable p = &x; // p stores address of variable x float *p1, *p2, *p3; //declares three float pointers p1, p2, p3, but are not assigned anything Example of float pointer:
#include<iostream>
using namespace std;
int main( )
{
float x = 30.7; // declaring float variable x and assigning 30.7 to it
float *Mars; // declaring float pointer, whose name is Mars
Mars = &x; // now hosanna points to variable x or stores address of variable x
cout<<x;
cout<<Mars;
cout<<*Mars<<endl; // prints value at address location Mars
cout<<&x<<endl; // same as printing value of pointer
cout<<*(&x); //same as printing value of x, which is 30.7
}
Char pointer:
Character pointer means that the pointer stores address of character value
Syntax of declaring char pointers:
char x = ‘A’; // x is an char variable, which stores character A char *p; // p is a pointer variable p = &x; // p stores address of variable x char *p1, *p2, *p3; //declares three char pointers p1, p2, p3, but are not assigned anything Example of char pointer:
#include<iostream> using namespace std; int main( ) { char x = 'T'; // declaring float variable x and assigning character T to it char *hosanna; // declaring float pointer, whose name is hosanna hosanna = &x; cout<<x<<endl; cout<< long(hosanna)<<endl; cout<<* hosanna<<endl; // prints value at address location hosanna cout<<(int)&x<<endl; // same as printing value of pointer cout<<*(&x); //same as printing value of x, which is T }
Void pointer:
Void pointer is a general purpose pointer, which can store address of any type. It can store address of integer data/float data/char data.
Syntax of declaring void pointers:
int a; // a is an integer variable float b; // b is a float variable char c; // c is a character variable void *ptr1, *ptr2, *ptr3; // declaration of 3 void type pointers ptr1 = &a; // void pointer ptr1 stores address of variable a ptr2 = &b; // void pointer ptr2 stores address of variable b ptr3 = &c; // void pointer ptr3 stores address of variable c Example of void pointer:
#include<iostream> using namespace std; int main( ) { int a = 2; float b =5.9; char c = 'X'; void *ptr1, *ptr2, *ptr3; // declaration of 3 void type pointers ptr1 = &a; ptr2 = &b; ptr3 = &c; cout<<a<<endl<<b<<endl<<c<<endl; cout<<ptr1<<endl; // prints pointer address ptr1; cout<< ptr2<<endl; // prints pointer address ptr2 cout<< ptr3; // prints pointer address ptr3 }
Arrays & Pointers
#include<iostream>
using namespace std;
int main( )
{
int num[ ] = { 24, 34, 12, 44, 56, 17 } ;
int i ;
for ( i = 0 ; i <= 5 ; i++ )
{
cout<<“element no.”<<i;
cout<<“address =”<<&num[i]<<endl;
}
}
The output of this program would look like this:
element no. 0 address = 65512
element no. 1 address = 65514
element no. 2 address = 65516
element no. 3 address = 65518
element no. 4 address = 65520
element no. 5 address = 65522