×

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

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs. Firstly, we can learn the range of int: The int is...

3 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

Free() Function in C

Free() function is a built-in function which is define in the stdlib.h header file. If we want to use this function in our program, we must include the stdlib.h header...

4 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 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.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.