What is the use of pointers, for example:
int var;
int *p;
int **pp;
var = 50;
I even understand the use of the simple pointer (*), but why use another to reference this?
What is the use of pointers, for example:
int var;
int *p;
int **pp;
var = 50;
I even understand the use of the simple pointer (*), but why use another to reference this?
Suppose an 8-bit computer with 8-bit addresses and only 256 bytes of memory. This is part of memory, the numbers above represent the addresses:
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| | 58 | | | 63 | | 55 | | | h | e | l | l | o | const char *c = "hello";
| |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
We can notice that at address 63 the string "hello" starts. So in that case, if this is the only occurrence of "hello" in memory then
const char **cp = &c;
... defines c
as the read-only pointer of the string "hello", and contains the value 63. c
must be stored somewhere: in the above example it is located in the address 58. In addition to pointing to characters, but we can also point to pointers. EX:
const char ***cpp = &cp;
Now cp
points to c
, That is, It contains the address of c
(that is 58). Consider also:
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| | 58 | | | 63 | | 55 | | | h | e | l | l | o | const char *c = "hello";
| |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
Now cpp
stores the cp
address. So its value is 55 based on the example above, and it stores the address 60 itself.
Now the reason for having pointers:
The name of an array gives the address of its first element. So if the array contains elements of type 't', a reference to the array has type 't *'. Now consider an array of 't' arrays: of course a reference to this array 2d will be of type '(t *) *' = 't **', and hence a pointer pointer.
li>Even though an array of strings sounds like 1D. in fact it is 2D, since the strings are arrays of characters. Consequently: 'char **'.
An 'f' function will have to accept a parameter of type 't **' if it is to change the variable of type 't *'.
Among many other utilities.
This information was translated from Stephan's post: link
One of the main uses of int **p
is the construction of matrices. A int *p
pointer can point to an array of integers (multiple integers in contiguous memory positions) and a "pointer pointer" can point to an array of pointers, each pointing to an array of integers. Note that this can do an array (array of arrays) with lines of different size.
In addition, another common use is to pass the pointer as a reference to a function. This way you can pass a structure as a parameter to a function and it can change where your pointer points internally (such as pointing to another place because you had to use malloc internally to increase its size).