"4. ... meaning that handle points to ptr?"
yes! ok.
"5. And then **handle = num; confuses me, but I interpreted it as: the pointer to which handle is pointing (that would be ptr) is now pointing to num"
Not correct: here there is the assignment of the value of num
to (ie: num
is copied into) the location pointed by the location pointed by handle
. The location pointed by handle
is ptr
per the previous line, so num
is copied into the the location pointed by ptr
, that is the 1st of the two location allocated with malloc()
.
"6. ... would be now pointing to num+1"
the conclusion is correct, but you are not pointing to num+1
. You have assigned/copied num+1
into *(*handle+1)
. Since *handle
is ptr
, into *(ptr+1)
or if you prefer the effect is the same of ptr[1]=num+1
- where ptr
is still pointing to the memory allocated with malloc()
.
"7.Then *ptr = num-2; I assumed that would affect the variable num itself, working asnum = num - 2;` and therefore changing its value to 12. So, at this point, we'd have ptr pointing to the array composed of 14 and 15 and num having the value 12.
nope: read "*" as "the object pointed by" - it's a good mnemonic. So you are still altering the object pointed by ptr
, that is ptr[0] = num-2
, you are changing again the first of the two integer locations allocated with malloc()
.
Only at the next line (ptr = #
) you are changing the value of ptr
to the address of num
and operations on *ptr
, the object pointed by ptr
, are going to affect num
now on.