views:

1711

answers:

7

Given the following:

&row->count

Would &(row->count) be evaluated or (&row)->count be evaluated in C++?

EDIT: Here's a great link for C++ precedence.

+1  A: 

This is already asked. But here is a link.

Edit: Ok this question is very similar. And possibly there is an other one.

Gamecat
Can you provide a link to that question? Could this question be better worded to avoid the duplicated nature of this post?
stung
I can search for the question.
Gamecat
A: 

&(row->count)

andy.gurin
+6  A: 

&(row->count)

Firas Assaad
A: 

-> has a higher priority than & (address of). So your expression would be evalutated as &(row->count)

mxg
+1  A: 

C operator precendence is explained here

As per the table, -> is higher priority than the & operator, so it's &(row->count)

Mark
+13  A: 

As far as precedence rules go, I've always liked the one put forth by Steve Oualline in "Practical C":

There are fifteen precedence rules in C (&& comes before || comes before ?:). The practical programmer reduces these to two:

1) Multiplication and division come before addition and subtraction.

2) Put parentheses around everything else.

Michael Burr
Aaron
I won't argue - it's just a nice rule of thumb for me. I think that using Pascal way back when screwed me up on operator precedence because the logical and/or operators had a higher precedence than equality/relational operators. Screwed me up for life.
Michael Burr
If you use parentheses as in `(foo == bar) || (baz == qux)`, you are literally worse than LISP.
ben
@ben: Literally worse than LISP? Really? Of course, like most generalizations, the one I gave in the answer one should be used with a measure of common sense. Maybe read it as, "if you might wonder if precedence is a factor, use parens". That said, I actually like `if ((foo == bar) || (baz == qux))` better than `if (foo == bar || baz == qux)`, but not not enough to care very much.
Michael Burr
+1  A: 

May I suggest that you resolve such questions using a test programme? That has the advantage that you will know for sure that the answer is correct for your implementation, and you are not exposed to the risk of badly answered questions.

Marcin