Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
3.56/5 (3 votes)
See more:
Prefix increment/decrement's Precedence is lower than Array subscripting?
I referenced it from here[^]

I'm puzzled, how to explain this expression?
C++
int buff[5] = { 10, 20, 30, 40, 50 };
int idx = 0;
int num = buff[++idx];
assert(num == buff[1]);

as the result shows, ++ excute first then []. but it conflict with the precedence table as I referenced.

anyone knows? thanks very much.
Posted

The precedence table is only consulted within an expression, it has no effect across expressions. Expressions are completely evaluated before their results are used.

An array de-reference is a composite expression. It consists of two component expressions, one outside the square brackets and one inside:

base_address_expression[ index_expression ]

index_expression is completely evaluated before it's used as part of the overall expression:

const auto t = index_expression;
base_address_expression[ t ]

This happens whatever index_expression is - a function call, another array dereference or another complex arithmetic expression. It get's calculated first, completely, then used.
 
Share this answer
 
Comments
BCN-163 7-Jan-14 4:51am    
great work man, thanks very very much!
CPallini 7-Jan-14 8:10am    
5.
This is not a case of precedence, but of evaluating function arguments.operator[] is a function with one argument, and this argument must always be evaluated before passing it to the function. Since in this case the argument is an expression, the result of the expression is used as an argument. You could use the postfix increment instead if you wanted to pass 0, rather than 1.

The operator precedence rules would only be applied if you used the increment operator on buff like this:
C++
int num = ++buff[idx];


P.S. - an explanation of my first statement:
In C++ you can overload definitions for operators working on arguments of a specific class. This is called operator overloading. There are plenty of articles on this topic, e.g. here[^], but the one thing I was referring to is that when you overload the [] operator, the signature of the function must look like that of a function with one argument: the index value of the element you want to access.

That means whenever you invoke the [] operator, it is internally treated like a call to a function, and the first thing that is done then is that it's argument, the index value, is evaluated.
 
Share this answer
 
v3
Comments
AlwaysLearningNewStuff 6-Jan-14 6:22am    
Well explained. My +5. Best regards.
BCN-163 7-Jan-14 4:54am    
thanks very much, I know what you mean,but "operator[] is a function with one argument" makes puzzled, [] is an operator anyway.
Stefan_Lang 7-Jan-14 8:04am    
I've added a P.S. in my solution to explain this statement. If you still have trouble understanding, be sure to follow and read the link I posted, too.
BCN-163 12-Jan-14 8:02am    
thanks man! really great explanation

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900