int a = 5;
int *b = &a;
int **c = &b;
int ***e = &c;
void func(int d)
{
d++;
}
func(a) // a will stay the same, func will just copy it in d,
// increment it and garbage collect it after exiting
Address | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
Variable | b |
d |
a |
e |
c |
Value | 2 |
6 |
5 |
4 |
0 |
Content | &a |
6 |
5 |
&c |
&b |
Note: d
is garbage collected after exiting func
. Addressing is arbitrary and is just given as an example.
As a rule of thumb, balance asterisks: when the variable gains an asterisk, the type loses one, and vice-versa. Ampersands add asterisks.
Variable | Type |
---|---|
a |
int |
b |
int* |
c |
int** |
e |
int*** |
*b |
int |
*c |
int* |
**c |
int |
*e |
int** |
**e |
int* |
***e |
int |
&a |
int* |
&b |
int** |
&c |
int*** |
&e |
int**** |