Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Cpp programming Pointer to pointer

Posted on January 14, 2017December 27, 2019

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.

Cpp programming Pointer to 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;

}

Cpp programming Pointer to pointer
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

Cpp programming Pointer to pointer
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;
}

Cpp programming Pointer to pointer
Example program
Output

Related Links

Pointers in C Programming language

Pointers in C++ Programming language

Pointer to pointer in C Programming Language

Pointers with one d array in C language

Structure with pointers in C Language

Structure with pointers in C++ Language

Pointers and one d array in C++

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes