Precedence of operators with pointers

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

i = pti || ptj; 

?

In addition, I've read that summing the subtraction between pointers (pti+pj or pti-pj , for example) results in integers. Why does this happen?

    
asked by anonymous 03.08.2015 / 14:46

1 answer

3

I think you're wondering what the operators mean.

== is the equality 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 it would get the same address and this would happen if the two pointers pointed to the same variable, in this case.

|| is the logical operator of OU, that is, it tests if both are true, in case it would only give 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 is only for demonstration purposes.

If the question about precedence still persists, you have a table in that question . Note that the assignment operator has one of the lowest precedence, and it is almost certain that it will be executed last in common expressions (only the separation of expressions has lower priority to allow multiple declarations.

See more in the question about the C's OR or in this one about PHP but the principle is the same and still about JavaScript .

Jorge B. has already talked about the sum of pointers in the comment that is impossible.

    
03.08.2015 / 15:07