Learn about C Program to use Pointer to pointer(**) in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
C Program to use Pointer to pointer(**):
A pointer is used to store the address of a variable. Similarly, we must use ** to hold the address of a pointer (pointer to pointer). ** A pointer that holds the address of another pointer.
** is used to print the value contained in the pointer to pointer variable.
#include <stdio.h>
int main()
{
char ch = 'a'; // create a variable
char *ptr = &ch; // create a pointer to store the address of ch
char **ptrToPtr = &ptr; // create a pointer to store the address of ch
printf("Address of ch: %p\n", &ch); // prints address of ch
printf("Value of ch: %c\n", ch); // prints 'a'
printf("\nValue of ptr: %p\n", ptr); // prints the address of ch
printf("Address of ptr: %p\n", &ptr); // prints address
printf("\nValue of ptrToPtr: %p\n", ptrToPtr); // prints the address of ptr
printf("*ptrToPtr(Address of ch): %p\n", *ptrToPtr); // prints the address of ch
printf("**ptrToPtr(Value of ch): %c\n", **ptrToPtr); // prints ch
}
Output:
Address of ch: 000000000061FE17
Value of ch: aValue of ptr: 000000000061FE17
Address of ptr: 000000000061FE08Value of ptrToPtr: 000000000061FE08
*ptrToPtr(Address of ch): 000000000061FE17
**ptrToPtr(Value of ch): a
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Also read:
Sum of N elements using malloc() and free()
How to set a pointer to an offset in C