pointeur de pointeur en language c
A pointer to a pointer, also known as a double pointer, is a concept in the C programming language that allows you to indirectly access and modify the value of a pointer variable. It is denoted by the use of two asterisks ().
To understand the concept of a pointer to a pointer, let's break it down into steps:
Step 1: Declare a pointer variable
First, you need to declare a pointer variable. This is done by using the asterisk () symbol before the variable name. For example, int
ptr; declares a pointer variable named ptr
that can hold the memory address of an integer variable.
Step 2: Assign a memory address to the pointer variable
Next, you assign a memory address to the pointer variable. This is done by using the ampersand (&) operator followed by the variable name. For example, int num = 10;
and then ptr = #
assigns the memory address of num
to the pointer variable ptr
.
Step 3: Declare a pointer to a pointer
Now, to declare a pointer to a pointer, you need to use two asterisks () before the variable name. For example, int
ptr_to_ptr; declares a pointer to a pointer variable named ptr_to_ptr
.
Step 4: Assign the address of the pointer variable to the pointer to a pointer variable
To assign the address of the pointer variable to the pointer to a pointer variable, you use the ampersand (&) operator before the pointer variable name. For example, ptr_to_ptr = &ptr;
assigns the address of ptr
to ptr_to_ptr
.
Step 5: Access and modify the value using the pointer to a pointer
Finally, you can access and modify the value of the original variable using the pointer to a pointer. To access the value, you use the asterisk () operator before the pointer to a pointer variable name. For example, *ptr_to_ptr gives you the value of
num
.
Here's a complete example to illustrate the steps:
#include <stdio.h>
int main() {
int num = 10;
int *ptr;
int ptr_to_ptr;
ptr = #
ptr_to_ptr = &ptr;
printf("Value of num: %d\n", ptr_to_ptr);
return 0;
}
In this example, ptr_to_ptr
is a pointer to a pointer to an integer. It first points to ptr
, which in turn points to num
. By dereferencing ptr_to_ptr
twice (ptr_to_ptr
), we can access the value of num
indirectly.
I hope this explanation helps! Let me know if you have any further questions.