Memory in a program is divided into two parts: static and dynamic. Static memory refers to variables of a fixed size throughout the course of the program, whereas dynamic memory adapts to the size of variables that the program needs. For example, if a program needs to store a variable number of strings, dynamic memory would be used. In C language, double pointers (**) are used when working with dynamic memory.
What Is a Double Pointer (**)?
A double pointer (**) is a pointer that points to the location of another pointer. The location of the variable in memory is indicated by this additional pointer. As a result, when a double pointer (**) is assigned to a variable, two levels of memory address are created. This allows the C language to access dynamic memory more easily. On the memory stack, a double pointer (**) takes up the same amount of room as a regular pointer.
Let’s look at the syntax of a double pointer (**):
Now let’s look at an example of using double pointer (**) in a C program.
int main()
{
int val = 36;
int* ptr1;
int** ptr2;
ptr1 = &val;
ptr2 = &ptr1;
printf("Value of val is: %d\n", val);
printf("Value of val using single pointer is: %d\n", *ptr1);
printf("Value of val using double pointer is: %d\n", **ptr2);
return 0;
}
We are using the variable “val” with the value 36 in the above code. Then we point a pointer (ptr1) in the direction of the val, which is directed in the direction of a second pointer (ptr2) afterwards. But the pointers don’t change the value of the ‘val’, which remains 36.
Output
Double pointers (**) in the C programming language behave identically to regular pointers in C. As a result, the double-pointer (**) variable’s size and the regular pointer variable’s size are always identical.
int main()
{
int val = 36;
int* ptr1;
int** ptr2;
ptr1 = &val;
ptr2 = &ptr1;
printf(" Size of normal Pointer: %d \n", sizeof(ptr1));
printf(" Size of Double Pointer: %d \n", sizeof(ptr2));
return 0;
}
We are using the variable “val” with the value 36 in this code. Then we point a pointer (ptr1) in the direction of the val, which is directed in the direction of a second pointer (ptr2) afterwards. The size of both the pointers is calculated, which is identical, i.e. 8.
Output
What Does ** Do in C Language?
There are several functions of double pointers which are mentioned here:
1: Access to Dynamic Memory
The feature of double pointers (**) is what enables the C language to set up and access dynamic memory. To do this, a pointer is provided with an address that matches the first element of an array. The double pointer (**) then points to this pointer, allowing the data in this location to be read or written. As the program runs, more elements can be added to the array using a double pointer (**).
2: Define a Pointer Array
Another use for double pointers (**) is to define an array of pointers. This is useful when the program needs to access large amounts of data without worrying about the size of the array. A double pointer (**) can quickly access the address of these pointers, enabling the program to process the data without the trouble of resizing the array each time a value is changed.
3: Access to Memory Address Location
At the most basic level, the double pointer (**) simply gives the programmer access to the address of a given memory location. With this address, the programmer can then access any data stored in that location, or modify that data as required. To do this, the programmer simply dereferences the double pointer (**), which is done using the asterisk (*) character. Because of the double pointer (**), the programmer is now able to access and alter data in ways that were not before feasible.
4: Create Complex Data Structures
Furthermore, a double pointer (**) can also be used to create more complex data structures such as trees and graphs. Nodes, which can point to one or several additional nodes and make up a more sophisticated structure in trees and graphs. To create such a complex structure, the programmer must again define a double pointer (**) that points to the nodes in the data structure. The double pointer (**) allows the programmer to access and modify nodes in the structure in a way that would not be possible without the double pointer (**).
Conclusion
Double pointers (**) are a powerful part of C language, allowing programs to work with large volumes of data and dynamic memory. They provide the address of a piece of data for easy access and maintain the two levels of address throughout the passage of data. By utilizing double pointers (**), a C language program can quickly process large amounts of data without the need for resizing the array each time.