Home  >  Article  >  Backend Development  >  C program to show the relationship between pointers

C program to show the relationship between pointers

PHPz
PHPzforward
2023-09-08 23:45:021444browse

C program to show the relationship between pointers

In C programming language, a pointer to pointer or double pointer is a variable that holds the address of another pointer.

Declaration

Given below is the declaration of a pointer to a pointer -

datatype ** pointer_name;

For example int **p;

Here, p is a pointer to Pointer to pointer.

Initialization

'&' is used for initialization.

For example,

int a = 10;
int *p;
int **q;
p = &a;

Access

The indirect operator (*) is used to access

Sample program

The following is a double pointer C program-

Live demonstration
#include<stdio.h>
main ( ){
   int a = 10;
   int *p;
   int **q;
   p = &a;
   q = &p;
   printf("a =%d ",a);
   printf(" a value through pointer = %d", *p);
   printf(" a value through pointer to pointer = %d", **q);
}

Output

When the above program is executed, the following results will be produced-

a=10
a value through pointer = 10
a value through pointer to pointer = 10

Example

Now, consider another C program that shows pointer-to-pointer relationships.

Real-time demonstration

#include<stdio.h>
void main(){
   //Declaring variables and pointers//
   int a=10;
   int *p;
   p=&a;
   int **q;
   q=&p;
   //Printing required O/p//
   printf("Value of a is %d</p><p>",a);//10//
   printf("Address location of a is %d</p><p>",p);//address of a//
   printf("Value of p which is address location of a is %d</p><p>",*p);//10//
   printf("Address location of p is %d</p><p>",q);//address of p//
   printf("Value at address location q(which is address location of p) is %d</p><p>",*q);//address of a//
   printf("Value at address location p(which is address location of a) is %d</p><p>",**q);//10//
}

Output

When the above program is executed, the following results will be produced -

Value of a is 10
Address location of a is 6422036
Value of p which is address location of a is 10
Address location of p is 6422024
Value at address location q(which is address location of p) is 6422036
Value at address location p(which is address location of a) is 10

The above is the detailed content of C program to show the relationship between pointers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete