×

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable.

Once a pointer to another pointer is defined, the former pointer contains the address of the latter pointer, which then points to the location that contains the actual value.

Thus, the first pointer is used to store the address of the other variable, while the second pointer is used to store the address of the first variable.

It is due to this very reason they are termed double pointers.

Since a program can have pointers to int, pointers to float, pointers to char, and pointers to any structure in the C standard that are defined, it should not be too much a surprise when it comes to pointers to pointers.

However, when it comes to simple pointers and keeping them as simple as possible, the distinction between the pointer itself and what it points to is essential.

Pointer to pointer in C

A programmer should be able to think about pointers to other pointers even though we will have the difference between the pointer and what it points to and what that particular pointer points to.

Pointer to pointer can end up to many chain links in a similar way, such as pointers to pointers to pointers or pointers to pointers to pointers, but they do not have any practical use of it.

How is a pointer to pointer declared?

Declaring a pointer to another pointer is similar to declaring a pointer in the C standard. The only other difference is the inclusion of another asterisk before the full syntax.

int **ptr;

Any variable which is a pointer to another pointer must be defined in a similar way.

int **var;

E.g.:

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


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


var = 3000;


/* take the address of the variable ‘var’ */
ptr = &var;


/* take the address of pointer ‘ptr’ using the address of operator ‘&’ */
pptr = &ptr;


/* take the value using pptr */
printf("The value of var = %d\n", var );
printf("The value available at *ptr = %d\n", *ptr );
printf("The value available at **pptr = %d\n", **pptr);


return 0;
}

Output:

The value of var = 3000
The value available at *ptr = 3000
The value available at **pptr = 3000
#include<stdio.h>  
#include<conio.h>


void main ()  
{  


int x = 10;  
int *p;  
int **pp;   
p = &x; /* pointer p is now pointing to the address of the variable 'x' */  
pp = &p; /*pointer 'pp' is double pointer pointing to the address of the pointer p */ 


printf("The address of variable 'x' is: %x \n",p); /* Address of 'x' will be printed */  
printf("The address of pointer 'p' is: %x \n",pp); /*Address of 'p' will be printed  */


printf("The value stored at first pointer 'p' is: %d \n",*p); 
/*value stoted at the address contained by p will be printed  */


printf("The value stored at the second pointer 'pp': %d \n",**pp); 
/*value stored at the address contained by the pointer stored at pp  */
}

Output:

The address of variable 'x' is: 6e1cfe34 
The address of pointer 'p' is: 6e1cfe38 
The value stored at first pointer 'p' is: 10 
The value stored at the second pointer 'pp': 10 

In the above program, ‘p’ is the pointer variable that can hold the address of the variable ‘x’. In the same way, ‘pp’ is the variable that can hold the address of the variable ‘p’. It can withhold the address of the variable ‘x’.

The pointer ‘*pp’ gives the value of an address stored by the ‘pp’. The pointer ‘pp’ stores the address of the ‘p’ pointer, and the value at the address of ‘p’ is the address of the variable ‘x’.

Hence, ‘*pp’ prints the address of the variable ‘x’. The ‘**pp’, also read as ‘*(*pp),’ gives the stored value at the address ‘*pp’. It can be concluded that ‘*pp’ means the address of the variable ‘x’.

Referencing and dereferencing of a double pointer:

  1. Referencing: referencing means assigning the address to the pointer declared previously.
  2. Dereferencing: dereferencing means accessing the value that is stored in the pointer.

E.g.:

#include<stdio.h>  
#include<conio.h>


int main()
{
int x = 10;
int *referencing = &x;     /* references the variable x */
int **dereferencing = &referencing; /*dereferences the pointer */


printf("The address of x = %p\n",&x);
printf("referencing is pointing to the address = %p\n",referencing);
printf("dereferencing is pointing to the address = %p\n",dereferencing);
printf("The value of x = %d\n",x);
printf("*referencing = %d\n",*referencing);
printf("**dereferencing = %d\n",**dereferencing);


return 0;
}

Output:

The address of x = 0x7fff7c538284
referencing is pointing to the address = 0x7fff7c538284
dereferencing is pointing to the address = 0x7fff7c538288
The value of x = 10
*referencing = 10
**dereferencing = 10

Related Topics

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

2 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Difference between while and do-while loop in C

Introduction Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes. Both loops process the program/code in the...

3 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.