If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In C language, a pointer to a pointer is a type of pointer that holds the memory address of another pointer. It's represented by a double indirection operator (**). A pointer to a pointer is useful in situations where you need to pass a pointer to a function, but you also want the function to be able to modify the pointer itself.
Here's an example of a pointer to a pointer:
Copy code
#include <stdio.h>
int main() {
int x = 10;
int* p = &x;
int** pp = &p; // pp is a pointer to a pointer
printf("%d\n", **pp); // prints 10
return 0;
}
In this example, the variable x
is an integer, p
is a pointer that holds the memory address of x
, and pp
is a pointer to a pointer that holds the memory address of p
. To access the value of x
, we dereference pp
twice, first as a pointer and then as an integer.
Here's an example of a function that takes a pointer to a pointer as an argument and modifies the pointer:
Copy code
#include <stdio.h>
void func(int** pp) {
int y = 20;
*pp = &y;
}
int main() {
int x = 10;
int* p = &x;
func(&p);
printf("%d\n", *p); // prints 20
return 0;
}
In this example, the func
function takes a pointer to a pointer pp
as an argument. Inside the function, it creates a new integer y
with the value 20, and assigns the address of y
to the pointer pp
points to, thus changing the value of p
and the value it points to.
It's worth noting that, when working with pointer to pointer, you need to be
Comments: 0