Pointer to Pointer in C

CCBeginner
Practice Now

Introduction

In C programming, pointers are used to store the memory addresses of other variables. Similarly, a pointer to a pointer is a variable that stores the memory address of another pointer variable. In this lab, we will learn how to write a C program that deals with pointer to pointer.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123305{{"`Pointer to Pointer in C`"}} c/variables -.-> lab-123305{{"`Pointer to Pointer in C`"}} c/data_types -.-> lab-123305{{"`Pointer to Pointer in C`"}} c/operators -.-> lab-123305{{"`Pointer to Pointer in C`"}} c/memory_address -.-> lab-123305{{"`Pointer to Pointer in C`"}} c/pointers -.-> lab-123305{{"`Pointer to Pointer in C`"}} c/function_declaration -.-> lab-123305{{"`Pointer to Pointer in C`"}} end

Declare Necessary Variables

In this step, we will declare three variables; var, *ptr and **pptr.

int var;
int *ptr;
int **pptr;

Assign Value to Variable var

In this step, we will assign a value to the variable var.

var = 50;

Assign Address of var to Pointer ptr

In this step, we will assign the address of the variable var to the pointer ptr.

ptr = &var;

Assign Address of ptr to Pointer to Pointer pptr

Now, we will assign the address of the pointer ptr to the pointer to pointer variable pptr.

pptr = &ptr;

Access Value Using pptr

Finally, we will access the value of var using the pointer to pointer variable pptr.

printf("\nValue of var = %d\n", var);
printf("\nValue available at *ptr = %d\n", *ptr);
printf("\nValue available at **pptr = %d\n", **pptr);

The first printf statement will print the value of variable var.

The second printf statement will print the value available at the memory address stored in ptr, which should be the value of var.

The third printf will print the value available at the memory address stored in pptr, which should be the value of ptr, and then dereferencing twice to get the value of var.

Write Full Code in main.c

Now that we have gone through the various steps, we can put the code together in the main function of main.c.

#include <stdio.h>

int main()
{
    int var;
    int *ptr;
    int **pptr;

    var = 50;

    ptr = &var;

    pptr = &ptr;

    printf("Value of var = %d\n", var);
    printf("Value available at *ptr = %d\n", *ptr);
    printf("Value available at **pptr = %d\n", **pptr);

    return 0;
}

Summary

In this step-by-step lab, we have learned how to write a C program that deals with pointer to pointer. We have declared the necessary variables, assigned a value to the variable, assigned the address of the variable to the pointer, assigned the address of the pointer to the pointer to pointer, and finally accessed the value of the variable using the pointer to pointer. Being familiar with pointer to pointer is a crucial stepping stone in learning C programming.

Other C Tutorials you may like