Unknown terms pointer in C

Asked

Viewed 39 times

0

The first output of the printf is coming out a strange number, I think %p would be the address of the pointer would not be, the result gave 0060FEFC? in the second output *p + 2 = i + 2 I understand result gave 7, * *&p also not understood, I thought it would be pointer to pointer but did not understand the commercial and the result, left the value of *p that is 5 seems to ignored the rest, 3**p I understood it would be 3 * i all right result gave 15, e * *&p+4 did not understand the result or the wording.. someone could give a light, thank you

Edit1: it seems to me that the expression of two asteristics plus the commercial and the variable is equal to the most variable asteristico, I am sure of that?

Exercise. What will be the output of this program assuming i occupies address 4094 in memory?

int main() {
int i=5, *p;
p = &i;
printf("%p %d %d %d %d \n", p,*p+2,**&p,3**p,**&p+4);
}

1 answer

3


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..

  • @CI2727 edited the question supplementing with an explanation about how operators work together and what & returns.

  • now it’s perfect, thank you ruan!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.