Syntax:
It has the following syntax:
Datatype **pointer_name;
In this structure,
- Datatype: It signifies the data type being used.
- Pointer_name: It denotes the identifier for the pointer.
In the realm of C programming, declaring a double pointer follows a structure akin to declaring a single pointer. The primary contrast between a pointer and a double pointer lies in the inclusion of an extra asterisk (*) preceding the variable name of the pointer.
C Double Pointer Example
Let's consider a scenario to demonstrate the concept of double pointers in C programming.
Example
#include<stdio.h>
void main ()
{
int a = 10;
int *p;
int **pp;
p = &a; // pointer p is pointing to the address of a
pp = &p; // pointer pp is a double pointer pointing to the address of pointer p
printf("address of a: %x\n",p); // Address of a will be printed
printf("address of p: %x\n",pp); // Address of p will be printed
printf("value stored at p: %d\n",*p); // value stoted at the address contained by p i.e. 10 will be printed
printf("value stored at pp: %d\n",**pp); // value stored at the address contained by the pointer stoyred at pp
}
Output:
address of a: d26a8734
address of p: d26a8738
value stored at p: 10
value stored at pp: 10
Explanation:
In this instance, we illustrate the idea of a dual pointer in the C programming language. Specifically, a pointer named p is utilized to hold the memory address of variable a, whereas a double pointer named pp stores the address of pointer p. Dereferencing p allows access to the value of a (10), and *pp achieves the same result through double dereferencing, showcasing the indirect referencing capability of double pointers.
Another Example of a Double pointer in C
Let's consider another instance to demonstrate the concept of double pointers in the C programming language.
Example
#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
int **p2;//pointer to pointer
p=&number;//stores the address of number variable
p2=&p;
printf("Address of number variable is %x \n",&number);
printf("Address of p variable is %x \n",p);
printf("Value of *p variable is %d \n",*p);
printf("Address of p2 variable is %x \n",p2);
printf("Value of **p2 variable is %d \n",*p);
return 0;
}
Output:
Address of number variable is fff4
Address of p variable is fff4
Value of *p variable is 50
Address of p2 variable is fff2
Value of **p variable is 50
Explanation:
In this instance, we've declared an integer variable set to the value 50. Subsequently, a pointer variable, denoted as p, and a double pointer variable, p2 of integer type, were initialized. The pointer p points to the memory address of the integer variable "number", while the double pointer p2 stores the memory address of pointer p. Following this, the script displays the memory address stored in pointer p, the value held by *p (the pointer variable), and then proceeds to display the memory address contained in p2. Lastly, it outputs the value located at the memory address pointed to by the double pointer p.
Note: The behavior of a double pointer is similar to that of a normal pointer, and so the size of a double pointer is always equal to that of a normal pointer, i.e., on the basis of the OS and the CPU architecture. It means the size is 8 bytes for a 64-bit system, and 4 bytes for a 32-bit system.
Use of Double Pointer in C
There are several uses of the double pointer in C. Some main uses of a double pointer in C are as follows:
- It is utilized to store multilevel data, like the text document paragraph and word semantics.
- We can use a double pointer as method parameters for manipulating the address that is stored in the local pointer variable.
- It is also used to store multidimensional arrays in dynamic memory.
- They can be utilized in data structures as well to change the address of the nodes without the need to copy them.
Double Pointer with an Array of Pointers in C
Let's consider an illustration to demonstrate the concept of double pointers using an array of pointers in the C programming language.
Example
#include<stdio.h>
void main ()
{
int a[10] = {100, 206, 300, 409, 509, 601}; //Line 1
int *p[] = {a, a+1, a+2, a+3, a+4, a+5}; //Line 2
int **pp = p; //Line 3
pp++; // Line 4
printf("%d %d %d\n",pp-p,*pp - a,**pp); // Line 5
*pp++; // Line 6
printf("%d %d %d\n",pp-p,*pp - a,**pp); // Line 7
++*pp; // Line 8
printf("%d %d %d\n",pp-p,*pp - a,**pp); // Line 9
++**pp; // Line 10
printf("%d %d %d\n",pp-p,*pp - a,**pp); // Line 11
}
Output
1 1 206
2 2 300
2 3 409
2 3 410
Explanation:
In this illustration, we showcase the application of double pointers in conjunction with an array of pointers. In this scenario, an array 'a' is utilized to store integer values, while p is responsible for holding the addresses of each element within a. Following this, the double pointer pp is initially set to point to p, and pointer arithmetic operations (pp++, pp++, ++pp, ++**pp) are executed to navigate and modify both the pointers and the values in the array. Subsequently, the printf function is employed to exhibit the progressive alterations in the position of pp, the offset from the base array a, and the dereferenced value.
Double Pointer using Dynamic Memory Allocation
In C programming, double pointers can be employed with the malloc, calloc, and free functions to dynamically allocate memory to pointers within the functions.
C Double Pointer Example using Dynamic Memory Allocation
Let's consider a scenario to demonstrate the concept of double pointers in C programming with the utilization of dynamic memory allocation.
Example
#include <stdio.h>
#include <stdlib.h>
int main() { //main function
int r = 2, c = 3; //r= rows, c=columns
int **arr;
// Allocating the memory for row pointers
arr = (int **)malloc(r * sizeof(int *));
// Allocating the memory for every row
for (int i = 0; i < r; i++) {
arr[i] = (int *)malloc(c * sizeof(int));
}
int val = 2;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
arr[i][j] = val++;
}
}
printf("2D Array:
");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("%d ", arr[i][j]);
}
printf("
");
}
// Free allocated memory
for (int i = 0; i < r; i++) {
free(arr[i]);
}
free(arr);
return 0;
}
Output:
2D Array:
2 3 4
5 6 7
Conclusion
In summary, double pointers in C play a crucial role in managing pointers to pointers. They are valuable in various scenarios like allocating memory for 2D arrays dynamically, passing references to functions, managing data structures, and more. Additionally, they provide significant flexibility for memory management in C. Proper utilization of double pointers is essential to prevent issues like memory leaks and dangling pointers, ultimately enhancing the program's dynamism and efficiency.