Home > Backend Development > C++ > body text

C program to show the relationship between pointers

PHPz
Release: 2023-09-08 23:45:02
forward
1445 people have browsed it

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;
Copy after login

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;
Copy after login

Access

The indirect operator (*) is used to access

Sample program

The following is a double pointer C program-

< p> 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);
}
Copy after login

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
Copy after login

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//
}
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template