Precedence of operators with pointers

Asked

Viewed 139 times

5

Having, for example, the following instructions:

int i=10, j=20; 
int *pti, *ptj; 
pti = &i; 
ptj = &j; 

What is the meaning of

j = pti == ptj;

and of

i = pti || ptj; 

?

Also, I read that the sum subtraction between pointers (pti+pj or pti-pj, for example) results in integers. Why this happens?

  • 1

    pti+ptj gives build error.

  • Who wants to play with this: https://ideone.com/6pAjfC

1 answer

3


I think you’re wondering what the operators mean.

== is the equal operator, so it is checked if the pointers are equal and it is obvious that the result will be 0 (false) since it points to data in different variables. It would be 1 only if both were equal, ie would take the same address and this would occur if the two pointers pointed to the same variable, in this case.

|| is the logical operator of OU, that is, it tests whether both are true, in which case it would only be false if both were null, since for this operator to give 0 (false) the two addresses would have to be 0.

Obviously the result is being stored in the original variables. The code only has demonstration utility.

If doubt about precedence still persists has a table in this question. Note that the assignment operator has one of the lowest precedences and it is almost certain that it will run last in common expressions (only the expression separation has lower priority to allow multiple declarations.

See more on question about the C RO or in the other about PHP but the principle is the same and still about Javascript.

Jorge B. has already spoken about the sum of pointers being is impossible in the commentary.

Browser other questions tagged

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