Home » C programs » C pointer programs

C program to demonstrate example of double pointer (pointer to pointer)

In this C program, we are going to learn about double pointers (pointer to pointer) in C programming language, here we will learn how to declare, assign and use a double pointer (pointer to pointer) in C?
Submitted by IncludeHelp, on April 26, 2018

In this program, we have to declare, assign and access a double pointer (pointer to pointer) in C.

As we know that, pointers are the special type of variables that are used to store the address of another variable.

But, when we need to store the address of a pointer variable, we cannot assign it in a pointer variable. To store address of a pointer variable, we use a double pointer which is also known as pointer to pointer.

In this C program, we will understand how to declare, assign and access a double pointer (pointer to pointer)?

C program for double pointer (pointer to pointer)



/*C program to demonstrate example of double pointer (pointer to pointer).*/
#include <stdio.h>
 
int main()
{
    int a;          //integer variable
    int *p1;            //pointer to an integer
    int **p2;           //pointer to an integer pointer
     
    p1=&a;          //assign address of a
    p2=&p1;         //assign address of p1
     
    a=100;          //assign 100 to a
     
    //access the value of a using p1 and p2
    printf("\nValue of a (using p1): %d",*p1);
    printf("\nValue of a (using p2): %d",**p2);
     
    //change the value of a using p1
    *p1=200;
    printf("\nValue of a: %d",*p1);
    //change the value of a using p2
    **p2=200;
    printf("\nValue of a: %d",**p2);
     
    return 0;
}

Output

    Value of a (using p1): 100
    Value of a (using p2): 100
    Value of a: 200
    Value of a: 200




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.