views:

131

answers:

3

Simply, is

&someObject->someAttribute.someMember;

equivalent to

&(someObject->someAttribute.someMember);

or

(&someObject)->someAttribute.someMember;

or

(&(someObject->someAttribute)).someMember;

Or should you really put explicit parenthesis there just to be safe?

+10  A: 

It is equivalent to:

&((someObject->someAttribute).someMember)

The -> and . have equal precedence and are left-associative. The unary-& has lower precedence.

If you have very complex expressions, then you should certainly use parentheses to group things and clarify your code. This isn't particularly complex, though; it is quite common to see code like this in C and C++.

James McNellis
+4  A: 

See Wikipedia

Scroll down to the operators section. Operators in a higher box in the chart are considered before operators in a lower box. Also see the rules for associativity.

Or should you really put explicit parenthesis there just to be safe?

This is somewhat of a preference issue but as a general rule, if you're having any trouble figuring it out, then someone else will too. Put parenthesis when in doubt.

dgnorton
-1 because you didn't actually answer the question, and it was a simple straightforward question. This should have been a comment instead of a reputation-fishing expedition. See Martin York's comment for an example.
John Dibling
@John: A bit harsh for such a new user. Teaching to fish is important too, and even preferable in some cases.
Roger Pate
@Roger: You're probably right. @dgnorton: Sorry for the harshness. I've undone my d/v.
John Dibling
A: 

If u want address of the someMember then &(someObject->someAttribute.someMember) OR &someObject->someAttribute.someMember.

First someObject->someAttribute and second to its member somMember and third is & address

struct xxx{ int xx; }; struct temp{ int x; struct xxx ptr; };

    struct temp t1 = {125,{127}};
    struct temp *t2 = &t1;
    printf("%d %d",t2->x,*&t2->ptr.xx);
Muthuraman