Types of Pointers in C++

Pointer is a derived data type that refers to another data variable by storing the variable’s memory address rather than data. A pointer variable defines where to get the value of a specific data variable instead of defining actual data.

Declaring & Initializing C++ Pointers:

It has the following syntax:

data-type *pointer-variable; // pointer-variable is the name of the pointer

Example:

int *ptr; // declaration
int x;
ptr=&x; // initialization

Program:

#include<iostream.h>
#include<conio.h>
void main()
{
int p, q, add, *ptr1, *ptr2;
clrscr();
cout<<"Enter the value of p :"; cin>>p;
cout<<"Enter the value of q :"; cin>>q;
ptr1=&p;
ptr2=&q;
add=*ptr1+*ptr2;
cout<<"The addition is :"<<add;
getch();
}

Output:
Enter the value of p: 15
Enter the value of q: 25
The addition is: 40

Array of Pointers:

We created space dynamically for two objects of equal size. But this may not be the case always. For example, the objects of a class that contain character strings would not be of the same size. In such cases, we can define an array of pointers to objects that can be used to access individual objects.

Program:

#include<iostream.h>
#include<conio.h>
using namespace std;
class city
{
protected:
char *name;
int len;
public:
city()
{
len=0;
name=new char[len+1];
}
void getname(void)
{
char *ch;
ch=new char[30];
cout<<"Enter city name:"; cin>>ch;
len=strlen(ch);
name=new char[len+1];
strcpy(name, ch);
}
void printname(void)
{
cout<<name<<"\n"; } }; int main() { city *sptr[10]; // array of pointers int n=1; int option; do { sptr[n]=new city; sptr[n]->getname();
n++;
cout<<"Do you want to more?";
cout<<"Enter 1 for yes and 0 for no:"; cin>>option;
}
while(option);
cout<<"\n\n";
for(int i=1;i<=n;i++) { sptr[i]->printname();
}
return 0;
}

Output:
Enter city name: Delhi
Do you want to more?
Enter 1 for yes and 0 for no: 1
Enter city name: Kolkata
Do you want to more?
Enter 1 for yes and 0 for no: 0

Delhi
Kolkata

Pointers to Functions:

In C++, It allows us to select a function dynamically at run-time. We can also pass a function as an argument to another function. Hence, the function is passed as a pointer. The pointer to the function is known as the Callback Function. C++ provides two types of function pointers:

1. Function pointers that point to the static member functions
2. Function pointers that point to non-static member functions

We can declare Pointers to Functions in C++, It has the following syntax:

data_type(*function_name)();

Example:

int (*num_function(int x));

Pointer To Object:

In C++, A pointer can point to an object created by a class. Object Pointers are useful in creating objects at run-time. So, we can also use an object pointer to access the public members of an object.

Program:

#include<iostream.h>
#include<conio.h>
using namespace std;
class item
{
int code;
float price;
public:
void getdata(int a, float b)
{
code=a;
price=b;
}
void show(void)
{
cout<<"Code :"<<code<<"\n";
cout<<"Price :"<<price<<"\n";
}
};
const int size=2;
int main()
{
item *p=new item[size];
item *ptr=p;
int x, i;
float y;
for(i=0;i<size;i++)
{
cout<<"Input code and price for item"<<i+1; cin>>x>>y;
p->getdata(x,y);
p++;
}
for(i=0;i<size;i++)
{
cout<<"Item:"<<i+1<<"\n"; ptr->show();
prt++;
}
return 0;
}

Output:
Input code and price for item1 50 700
Input code and price for item2 60 800
Item:1
Code : 50
Price : 700
Item:2
Code : 60
Price : 800

this Pointer:

In C++, this pointer is a unique keyword that is used to represent an object that invokes a member function. this pointer is a pointer that points to the object for which this function was called. The pointer acts as an implicit argument to all the member functions.

Example:

#include<iostream.h>
#include<conio.h>
using namespace std;
class person
{
char name[20];
float age;
public:
person(char *s, float a)
{
strcpy(name, s);
age=a;
}
person & person::greater(person &x)
{
if(x.age>=age)
return x;
else
return *this;
}
void display(void)
{
cout<<"Name:"<<name<<"\n"<<"Age:"<<age<<"\n";
}
};
int main()
{
person p1("Ram",35.5), p2("Shyam",28.5), p3("Jodhu",40.5);
person p=p1.greater(p3);
cout<<"Elder person is:\n";
p.display();
p=p1.greater(p2);
cout<<"Elder person is:\n";
p.display();
return 0;
}