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

C programming language pointer to pointer

Posted on January 14, 2017January 29, 2020

Table of Contents

  • C programming language pointer to pointer
  • When the code is executed, it produces the following result

C programming language pointer to pointer

In this tutorial, we will discuss the concept of the pointer to a pointer in C programming language.

Normally, the pointer is a special type of variable which stores the memory address of another variable. Pointer to pointer means one of the pointers contain the address of another pointer.

As shown above, x is a variable. It has the memory address 1004 and 1004 is stored at the ptr 1 (pointer 1). ptr 1 has the memory address 1008 which is stored in 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 <stdio.h>
#include <stdlib.h>

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
printf(“x value is: %dn”,x);  //display value of x
printf(“address of x is: %un”,&x);  //display address of x
printf(“the value of ptr1 is: %un”,ptr1); //display value of ptr1
printf(“the address of ptr1 is: %un”,&ptr1);  //display address of ptr1
printf(“the value pointed by the pointer ptr1 is: %un”,*ptr1);
printf(“the value of ptr2 is: %un”,ptr2);  //display value of ptr2
printf(“the address of ptr2 is: %un”,&ptr2); //display address of ptr2
printf(“the value of x by using pointer to a pointer is: %dn”,**ptr2);
return 0;
}

 

At above program,
Lets try to understand the above program
X value is 15;
Address in memory location of x is 2686748
The valur of ptr1(pointer1) is 2686748.
Address in memory location ptr1 (pointer 1) 2686744
The value pointed by ptr1(pointer 1) is 15
The valur of ptr1(pointer1) is 2686744
Address in memory location ptr1 (pointer 1) 2686740

memory location Diagram of above program

Program 2

#include <stdio.h>
#include <stdlib.h>

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
printf(“value of marks is: %dn”,marks);
printf(“value of marks pointed by ptr1 is: %dn”,*ptr1);
printf(“value of marks point to pointed by ptr2 is: %dn”,**ptr2);
printf(“………………..n”);
//find memory address of variable marks
printf(“address of marks is: %un”,&marks);
printf(“address of marks contain by ptr1 is: %un”,ptr1);
printf(“address of marks using by ptr2 is: %un”,*ptr2);
printf(“………………..n”);
//find value of pointer
printf(“value of pointer ptr1 is: %dn”,ptr1);
printf(“value of pointer ptr1 using ptr2 is: %dn”,ptr1);
printf(“………………..n”);
//find address of pointer ptr1
printf(“address of pointer ptr1 is: %un”,&ptr1);
printf(“address of pointer ptr1 using ptr2 is: %un”,ptr2);
printf(“………………..n”);
//pointer to pointer address and value is
printf(“valueof pointer ptr2 is: %dn”,ptr2);
printf(“address of pointer ptr2 : %un”,&ptr2);

return 0;

}

When the code is executed, it produces the following result

 

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