Table of Contents
hide
Example : A simple C++ program to display the address of the variables without using pointer.
#include <iostream>
using namespace std;
int main()
{
int num;
num=10;
cout<<"Address of variable is = "<<&num<<endl;
return 0;
}
Output :
Address of variable is = 0x6ffe1c
--------- OR ----------
#include <iostream>
using namespace std;
class A
{
int num1,num2;
public:
void input()
{
num1=10;num2=20;
cout<<"Address of variable are = "<<&num1<<" and "<<&num2<<endl;
}
};
int main()
{
A a;
a.input();
return 0;
}
Output :
Address of variable are = 0x6ffe10 and 0x6ffe14
Example : A C++ program to display the address of the array elements/variables using Array Act as Pointer concept (without using pointer).
#include <iostream>
using namespace std;
int main()
{
int arr[5];
cout<<"Array Variables Address are : "<< endl;
for (int i = 0; i < 5; i++)
{
cout << arr + i <<endl;
}
return 0;
}
Output :
Array Variables Address are :
0x6ffe00
0x6ffe04
0x6ffe08
0x6ffe0c
0x6ffe10
Example : A simple C++ program to display the address of the integer and float variables with the help of integer and float pointer.
#include <iostream>
using namespace std;
int main()
{
int x=10;
float y=10.20;
int *ptr1;
float *ptr2;
ptr1 = &x;
ptr2= &y;
cout <<"The address of integer value is = "<< endl ;
cout << &x << endl;
cout <<ptr1<< endl;
cout << endl << endl;
cout <<"The address of float value is = "<< endl;
cout << &y << endl;
cout << ptr2<< endl;
return 0;
}
Output :
The address of integer value is = 0x6ffe0c
0x6ffe0c
The address of float value is = 0x6ffe08
0x6ffe08
Example : A simple C++ program to display the address of the variables with the help of pointer/WILD pointer.
#include <iostream>
using namespace std;
int main()
{
int num;
int *ptr; //wild Pointer
num=10;
ptr=#
cout<<"Address of variable is = "<<&num<<" and "<<ptr<<endl;
return 0;
}
Output :
Address of variable is = 0x6ffe14 and 0x6ffe14.
--------- OR ----------
#include <iostream>
using namespace std;
class A
{
int num1,num2;
public:
void input()
{
int num;
int *ptr;
num=10;
ptr=#
cout<<"Address of variable is = "<<&num<<" and "<<ptr<<endl;
}
};
int main()
{
A a;
a.input();
return 0;
}
Output :
Address of variable is = 0x6ffe14 and 0x6ffe14.
Example : A simple C++ program to display the address of the any variables with the help of VOID pointer.
#include <iostream>
using namespace std;
int main()
{
void *ptr;
int x=12;
float f = 3.141;
ptr = &x;
cout << &x << endl;
cout << ptr;
cout << endl << endl;
ptr = &f;
cout << &f << endl;
cout << ptr;
return 0;
}
Example : A C++ program to display the concept of NULL pointer.
#include <iostream>
using namespace std;
int main()
{
int *ptr = NULL; // Null pointer declaration having no address stored in it.
cout<<"The address stored by ptr is = "<<ptr;
return 0;
}
Output :
The address stored by ptr is = 0
Example : A C++ program to display the concept of THIS pointer.
#include <iostream>
using namespace std;
class Example
{
private:
int num;
char ch;
public:
void input(int num, char ch)
{
this->num =num;
this->ch=ch;
}
void display()
{
cout<<num<<endl;
cout<<ch;
}
};
int main()
{
Example obj;
obj.input(50, 'R');
obj.display();
return 0;
}
Output :
50
R
Example : A C++ program to display the array elements with the help of pointers.
#include <iostream>
using namespace std;
int main()
{
int *ptr;
int arr[]={12, 82, 43, 40, 75, 62};
ptr = arr;
for(int i=0; i<=5;i++)
{
cout<<*ptr<<" ";
ptr++;
}
return 0;
}
Output :
12 82 43 40 75 62
Example : A C++ program to display the address of array elements with the help of both array and pointers.
#include <iostream>
using namespace std;
int main()
{
int x[5];
int *ptr;
cout << "The Address of Array Elements using Array Address Operator = " << endl;
for (int i = 0; i < 5; i++)
{
cout << " Address of x[" << i << "] = " << &x[i] << endl;
}
cout << "\nThe Address of Array Elements using Array(that act as Pointer itself) = " << endl;
for (int i = 0; i < 5; i++)
{
cout << "Address of x[" << i << "] = " << x+i << endl;
}
ptr = x;
cout<<"\nThe Address of Array Elements using Pointer = "<< endl;
for (int i = 0; i < 5; i++)
{
cout << " Address of x[" << i << "] = " << ptr + i << endl;
}
return 0;
}
Output :
The Address of Array Elements using Array Address Operator =
Address of x[0] = 0x6ffde0
Address of x[1] = 0x6ffde4
Address of x[2] = 0x6ffde8
Address of x[3] = 0x6ffdec
Address of x[4] = 0x6ffdf0
The Address of Array Elements using Array(that act as Pointer itself) =
Address of x[0] = 0x6ffde0
Address of x[1] = 0x6ffde4
Address of x[2] = 0x6ffde8
Address of x[3] = 0x6ffdec
Address of x[4] = 0x6ffdf0
The Address of Array Elements using Pointer =
Address of x[0] = 0x6ffde0
Address of x[1] = 0x6ffde4
Address of x[2] = 0x6ffde8
Address of x[3] = 0x6ffdec
Address of x[4] = 0x6ffdf0
Example : A C++ program to display the address & values of array elements with the help of Array of pointer concept.
#include <iostream>
using namespace std;
int main()
{
int x=10,y=20,z=30;
int *ptr[3]; // Array of pointer
ptr[0] = &x;
ptr[1] = &y;
ptr[2] = &z;
cout <<"\n Total Values are : ";
for(int i=0;i<3;i++)
{
cout << *ptr[i]<<" ";
}
cout <<"\n Total Addresses are : ";
for(int i=0;i<3;i++)
{
cout << ptr[i]<<" ";
}
return 0;
}
Output :
Total Values are : 10 20 30
Total Addresses are : 0x6ffe14 0x6ffe10 0x6ffe0c
--------- OR ----------
#include <iostream>
using namespace std;
int main()
{
int x[3];
int *ptr[3]; // Array of pointer
cout<<"Enter 3 array values : ";
for(int i=0;i<3;i++)
{
cin>>x[i];
}
for(int i=0;i<3;i++)
{
//ptr[i]= &x[i];
ptr[i]= x+i;
}
cout <<"\n Total Addresses are : ";
for(int i=0;i<3;i++)
{
cout << ptr[i]<<" ";
}
return 0;
}
Output :
Enter 3 array values :
10
20
30
Total Addresses are : 0x6ffe00 0x6ffe04 0x6ffe08
0 Comments