Pointer to Pointer in C Programming [Complete Guide]

pointer to pointer in c programming

As we have learned that we can declare a pointer to any data type. Same as we can also declare a pointer to any pointer.
A pointer is just a reference to any variable (We have discussed). If we declare a pointer just as
int *numberPtr;
(Read from right to left numberPtr is a pointer to an int)

This will make a block in memory that can contain the address of an integer variable or we can say numberPtr can point to an integer variable.

If we declare and initialize an integer variable just as
int xyz = 7;
and assign the address of that variable to the pointer numberPtr just as
numberPtr = &xyz;
then the numberPtr will point to the integer variable xyz as shown in the figure
a pointer is pointing to a variable
Now the original topic starts.

We have revised that how we can declare and initialize a pointer with the address of any data type (int, float, char, double etc).
We can also initialize a pointer with the address of another pointer or one pointer can point to one another pointer.

If we are declaring a pointer for a simple variable, we just use the data type of which we are declaring a pointer, one asterisk and the name of the pointer.

But if we want to declare a pointer for a pointer we have to use two asterisk signs.

For example, if I want to declare a pointer for a pointer of int. I will write
int **ptr;
(Read from right to left, ptr is a pointer to the pointer of int)

Now, I can assign the address of an integer pointer to ptr. If I want to assign the address of pointer numberPtr to the pointer of pointer ptr, then I will write
ptr = &numberPtr;
Now ptr has the address of numberPtr and numberPtr has the address of integer variable xyz and xyz has number 7 in it.

You can understand by understanding the following picture
pointer to pointer memory representation
Printing the value of Pointer of pointer:
printf("Value of xyz using the double pointer: %d",**ptr);
Output:
Value of xyz using the double pointer: 7

Complete program for better understading:

#include<stdio.h>
int main(){

    // Declaring and initializing integere variable
    int xyz = 7;

    // Declaring pointer of integer
    int *numberPtr;

    // Declaring Pointer of Pointer
    int **ptr;

    // Assigning the address of variable xyz to the pointer numberPtr
    numberPtr = &xyz;

    // Assigning the address of Pointer numberPtr to the Pointer Ptr
    ptr = &numberPtr;

    printf("Value of xyz: %d\n",xyz);
    printf("Value of xyz using the single pointer:%d\n",*numberPtr);
    printf("Value of xyz using the double pointer: %d\n",**ptr);

    return 0;

}

Output:
Value of xyz: 7
Value of xyz using the single pointer: 7
Value of xyz using the double pointer: 7

Comments

Popular posts from this blog

Change your Application background in Scene Builder || JavaFX FXML Tutorial

5 Programming Languages To Learn in 2019

c/c++ Programming questions || Practice Problems for Beginners || C Programs List