Categories: C++Learn C++

Cpp programming Pointer to pointer

Cpp programming Pointer to pointer

In this tutorial, we will discuss the concept of Cpp programming Pointer to pointer

Pointer is a special feature of C++. To understand pointer, you should know how data is saved in memory of the location. Normally, pointer is a special type of variable which stores the memory address of another variable. Pointer to pointer means one of the pointer contains the address of another pointer.

pointer to pointer

In the example above, x is a variable. It has the memory address 1004, 1004 is stored at the ptr 1 (pointer 1). ptr 1 has the memory address 1008 which is stored ptr 2(pointer 2).

 How to declare a pointer in C++ programming?

int *ptr;

How to declare a pointer to pointer in C programming?

int **ptr;


Program 1

#include <iostream>
using namespace std;
int main()
{
int x=15;  //assign int variable x and initialazed value
int *ptr1;  //assign a pointer variable ptr1 as int
ptr1=&x; //assign address of x to ptr1
int **ptr2;  //assign a pointer to pointer variable ptr2 as int
ptr2=&ptr1;  //assign address of ptr1 to ptr2
cout <<“x value is:”<<x<< endl;  //display value of x
cout <<“address of x is:”<<&x<< endl; //display address of x
cout <<“the value of ptr1 is:”<<ptr1<< endl; //display value of ptr1
cout <<“the address of ptr1 is:”<<&ptr1<< endl; //display address of ptr1
cout <<“the value pointed by the pointer ptr1 is:”<<*ptr1<< endl;
cout <<“the value of ptr2 is:”<<ptr2<< endl;  //display value of ptr2
cout <<“the address of ptr2 is:”<<&ptr2<< endl;//display address of ptr2
cout <<“the value of x by using pointer to a pointer is:”<<**ptr2<< endl;
return 0;

}

Example

In the above program,
We will try to understand the above program
X’s value is 15;
Address of memory location of x is 0x28ff0c
The value of ptr1 is 0x28ff0c.
The address of ptr1 is 0x28ff08.
The value pointer by the pointer ptr1 15
The address of ptr2(pointer1) is 0x28ff04.

Memory location diagram of the above program

pointer to pointer

 

Program 2

#include <iostream>
using namespace std;
int main()
{
int marks=99; //assign and initialized marks as integer
int *ptr1; //assign pointer for marks as ptr1;
int **ptr2;// double pointer for ptr1(pointer to pointer);
ptr1=&marks; //assign address of marks to ptr1
ptr2=&ptr1;
//find value of variable marks
cout <<“value of marks is:”<<marks<< endl;
cout <<“value of marks pointed by ptr1 is:”<<*ptr1<< endl;
cout <<“value of marks point to pointed by ptr2 is:”<< **ptr2<< endl;
cout <<“………………..”<< endl;
//find memory address of variable marks
cout <<“address of marks is:”<<&marks<< endl;
cout <<“address of marks contain by ptr1 is:”<<ptr1<< endl;
cout <<“address of marks using by ptr2 is:”<<*ptr2<< endl;
cout <<“………………..”<< endl;
//find value of pointer
cout <<“value of pointer ptr1 is:”<< ptr1<< endl;
cout <<“value of pointer ptr1 using ptr2 is:”<< ptr1<< endl;
cout <<“………………..”<< endl;
//find address of pointer ptr1
cout <<“address of pointer ptr1 is:”<<&ptr1<< endl;
cout <<“address of pointer ptr1 using ptr2 is:”<<  ptr2<< endl;
cout <<“………………..”<< endl;;
//pointer to pointer address and value is
cout <<“valueof pointer ptr2 is:”<< ptr2<< endl;
cout <<“address of pointer ptr2 :”<<&ptr2<< endl;

return 0;
}

Example program
Output
Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.