For each case:
p
The format %p
is used to make use of a formatting for displaying an address in memory. A pointer, being the variable p
in this case, it already returns the memory address to which it is pointing, so it is passed in the mentioned format returning a value similar to the example below:
0x07ffe...
*p+2
When you use the operator *
on a pointer that had already been declared you are accessing the content to which it is pointing. As the pointer in the example was pointing to a value of 5
, the result would be 7:
*p -> 5 -> 5 + 2 -> 7
**&p
The operator &
returns the memory address of some allocated value; using this in a pointer the return value is a pointer to a pointer, being its own region in memory. To clarify how to access a pointer:
p
-> Accesses the memory region to where p
is pointing;
&p
-> Accesses the memory region of the pointer. It is understood that the operator &
already returns a pointer to a value, then using the operator in p
we get a pointer to a memory region that in turn points to some value;
*p
-> Accesses the value where p
is pointing; in this case, would be the value 5
.
That is, when assessing the value to which it is pointing, it is necessary to use the operator *
twice. Consider the explanation:
&p
-> Returns the pointer address when using the operator &
one
pointer is returned, so we have a pointer to a pointer.
The value returned would be the memory address pointer: 0x007fe...
;
*&p
-> Returns the address to which a pointer points. Equivalent to write only p
in this case. Then the address to where p
point is
returned (the address of your variable i
): 0x007fc...
;
**&p
-> Like &p
returns the address to the pointer (0x007fe
) and *&p
gives us access to the address to which p
points out (0x007fc...
), we can conclude that using the operator *
again, we get the value where p
points out, being 5
. Writing equivalent *p
.
3**p
As already mentioned in the example with *p+2
, the operator *
will cause it to return the value to which the pointer is pointing. The pointer operator is evaluated in 5
which is the value to which the pointer is pointing, resulting in a multiplication operation (int
x int
):
*p -> 5 -> 3 * 5 -> 15
**&p+4
The previous examples should already make the final case clearer, using the concepts already seen in previous cases:
&p (ponteiro de um ponteiro, dois operadores * retornam seu valor) -> **&p -> 5 -> 5 + 4 -> 9
speaks Uan, thank you for your attention, I am not able to understand this part **&p, I can understand that &p is an address of the variable p, but alas these two asteristicos I did not understand.. by what I see speaking pointer to pointer are two asterisks and the variable, now this is commercial in the middle not understanding anything..
– Cl2727
@CI2727 edited the question supplementing with an explanation about how operators work together and what
&
returns.– Ruan Montelo
now it’s perfect, thank you ruan!
– Cl2727