Pointer to Pointer in C++ – theteche.com

Our next example demonstrates an array of pointers to objects, and shows how to sort these pointers based on data in the object. This involves the idea of pointer to pointer, and may help demonstrate why people lose sleep over pointers.

The idea in the next program is to create an array of pointers to objects of the person class. This is similar to the PTROBJS example, but we go further and add variations of the order() and bsort() functions from the PTRSORT example so that we can sort a group of person objects based on the alphabetical order of their names. Here’s the listing for PERSORT:

// persort.cpp
// sorts person objects using array of pointers
#include <iostream>
#include <string>     //for string class
using namespace std;
class person //class of persons
string name; //person’s name
void setName() //set the name
{ cout << “Enter name: “; cin >> name; }
void printName() //display the name
{ cout << endl << name; }
string getName() //return the name
{ return name; }
int main()
void bsort(person**, int); //prototype
person* persPtr[100]; //array of pointers to persons
int n = 0; //number of persons in array
char choice; //input char
do { //put persons in array
persPtr[n] = new person; //make new object
persPtr[n]->setName(); //set person’s name
n++; //count new person
cout << “Enter another (y/n)? “; //enter another
cin >> choice; // person?
while( choice==’y’ ); //quit on ‘n’
cout << “\nUnsorted list:”;
for(int j=0; j<n; j++) //print unsorted list
{ persPtr[j]->printName(); }
bsort(persPtr, n); //sort pointers
cout << “\nSorted list:”;
for(j=0; j<n; j++) //print sorted list
{ persPtr[j]->printName(); }
cout << endl;
return 0;
} //end main()
void bsort(person** pp, int n) //sort pointers to persons
void order(person**, person**); //prototype
int j, k; //indexes to array
for(j=0; j<n-1; j++) //outer loop
for(k=j+1; k<n; k++) //inner loop starts at outer
order(pp+j, pp+k); //order the pointer contents
void order(person** pp1, person** pp2) //orders two pointers
{ //if 1st larger than 2nd,
if( (*pp1)->getName() > (*pp2)->getName() )
person* tempptr = *pp1; //swap the pointers
*pp1 = *pp2;
*pp2 = tempptr;

When the program is first executed it asks for a name. When the user gives it one, it creates an object of type person and sets the name data in this object to the name entered by the user. The program also stores a pointer to the object in the persPtr array.

When the user types n to indicate that no more names will be entered, the program calls the bsort() function to sort the person objects based on their name member variables. Here’s some sample interaction with the program:

Enter name: Washington
Enter another (y/n)? y
Enter name: Adams
Enter another (y/n)? y
Enter name: Jefferson
Enter another (y/n)? y
Enter name: Madison
Enter another (y/n)? n
Unsorted list:
Sorted list:

Sorting Pointers

Actually, when we sort person objects, we don’t move the objects themselves; we move the pointers to the objects. This eliminates the need to shuffle the objects around in memory, which can be very time consuming if the objects are large. It could also, if we wanted, allow us to keep multiple sorts one by name and another by phone number, for example in memory at the same time without storing the objects multiple times.

Pointer to Pointer
Pointer to Pointer

To facilitate the sorting activity, we’ve added a getName() member function to the person class so we can access the names from order() to decide when to swap pointers.

Read More Topics
Pointers and array
Arrays programming in ANSI C
Passing pointer to function
Dynamic memory allocation

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply