Examples on Pointers

In the article, introduction to pointers, we understood some of the basic concepts on pointers. This article shows some of the examples on pointers. It will help us to enrich the knowledge on pointers.

Program 1:

This example shows the use of pointers. Integer value 10 is stored at a. Pointer p points to a. It stores the address of a. We get the value of a either by calling a or by de-referencing the pointer p. The value of the variable can also be changed using de-referencing p.

#include<iostream>
using namespace std;
int main(){
	int a=10;
	int* p;
	p = &a;
	cout<<"Value at a is "<<a<<endl;
	cout<<"De-referencing the value p "<<*p<<endl;
	cout<<"Value of pointer p is "<<p<<endl;
	cout<<"Address of a is "<<&a<<endl;
	cout<<"Changing value using dereferencing"<<endl;
	*p = 50;
	cout<<"Value at a is "<<a<<endl;
	cout<<"De-referencing the value p "<<*p<<endl;
	cout<<"Value of pointer p is "<<p<<endl;
	cout<<"Address of a is "<<&a<<endl;
	return 0;
}
Output of the above program

Program 2:

Incrementing the address variable. As the type of pointer is integer type, so when you increment the pointer, the value increase by 4 bytes, because the size of the integer is 4 bytes. So, when pn points to 100, pn+1 will point to 104 as shown in the figure below.

#include<iostream>
using namespace std;
int main(){
int a = 10;
	int* pn;
	pn = &a;
	cout<<"Value at pn is "<<pn<<endl;
	cout<<"Value at pn+1 is "<<pn+1<<endl;
	return 0;
}
Output of the above program

Program 3:

#include<bits/stdc++.h>
using namespace std;
int main(){
	int a = 45;
	cout<<"a = "<<a<<endl;
	cout<<"&a = "<<&a<<endl;
	char c = 'k';
	cout<<"c = "<<c<<endl;
	cout<<"&c = "<<(void*)&c<<endl;
	int* pa = &a;
	cout<<"pa = "<<pa<<endl;
	cout<<"*pa = "<<*pa<<endl;
	cout<<"&pa = "<<&pa<<endl;
	char* pc = &c;
	cout<<"pc = "<<(void*)pc<<endl;
	cout<<"*pc = "<<*pc<<endl;
	cout<<"&pc = "<<&pc<<endl;
	return 0;
}
Output of the above program

Program 4:

Using reference variable

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n = 10;
	int& pn = n;
	cout<<"n = "<<n<<" pn = "<<pn<<"\n";
	n = n+5;
	cout<<"n = "<<n<<" pn = "<<pn<<"\n";
	pn*=2;
	cout<<"n = "<<n<<" pn = "<<pn<<"\n";
	pn--;
	cout<<"n = "<<n<<" pn = "<<pn<<"\n";
	return 0;
}
Output of the above program

Program 5:

Using multiple reference

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n = 45;
	int& pn = n;
	int& pn2 = pn;
	int& pn3 = pn2;
	cout<<"n : "<<n<<" address : "<<&n<<endl;
	cout<<"pn : "<<pn<<" address : "<<&pn<<endl;
	cout<<"pn2 : "<<pn2<<" address : "<<&pn2<<endl;
	cout<<"pn3 : "<<pn3<<" address : "<<&pn3<<endl;
	return 0;
}
Output of the above program

Program 6:

Pointer to pointer

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n = 10;
	int* pn = &n;
	int** ppn = &pn;
	cout<<"n:"<<n<<" &n:"<<&n<<endl;
	cout<<"pn:\t"<<pn<<endl;
	cout<<"&pn:\t"<<&pn<<endl;
	cout<<"*pn:\t"<<*pn<<endl;
	cout<<"ppn:\t"<<ppn<<endl;
	cout<<"&ppn:\t"<<&ppn<<endl;
	cout<<"*ppn:\t"<<*ppn<<endl;
	cout<<"**ppn:\t"<<**ppn<<endl;
	return 0;
}
Output of the above program

One Reply to “Examples on Pointers”

Leave a Reply

Your email address will not be published. Required fields are marked *