We sometimes see this hungry code:
int *ptr = &x;
Here, PTR is a pointer to an X in-memory address.
Let's say that there is another statement like this:
int **ptr2 = &ptr;
We define a pointer to a pointer.
Suppose our computer is 8bit and the address is 8bit (and therefore only 256 bytes of memory). Represents a portion of memory (a row of numbers above represents an address).
45 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| | 58 | | | 63 | | 55 | | | h | e | l | l | o | \0 | |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
From the above you can see that address 63 is where the string "Hello" begins. In this case, suppose there is only one place in memory where "hello" is present, then:
Const Char " Hello ";
Define C as a pointer to the string "Hello", then the value inside C is 63. The pointer c itself is stored somewhere in the memory, and in the example above you can see where c is stored in 58. Now that we can define pointers to characters, we can define pointers to pointers:
Const char **CP = &c;
The CP now points to the address (58) where c is stored in the C,CP.
Can even be further:
Const char ***cpp = &cp;
CPP memory is the address of the CP, which is 55 (in the example above), the CPP itself is stored in the 60 location.
Why do we need these pointers?
- The array name is usually the address of the first element of the array. If the data type stored in the array is T, the type of the logarithmic group reference is * t. Suppose there is an array of arrays of type T (two-dimensional arrays), naturally, the reference type of the two-dimensional array is * (*t) =**t, which is the pointer to the pointer.
- Even if the array of strings looks like one-dimensional, it is actually two-dimensional because the string is an array of characters. Therefore, the type is char * *.
How does a pointer pointer work in the
C language?