2

I am confused of operator precedence table give in http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm

I mean right-to-left and left-to-right.

I want to know in what order the operator will we applied on this code.

int main()
{
   int i[] = {3, 5}; 
   int *p = i; 
   int j = --*p++; 

   printf("j = %d\n\n", j);
   system("pause");
   return 0;
}

Is it like --(*(p++))? or (--(*p))++ ? Its very confusing. is there any standard rule to resolve this problem.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
dead programmer
  • 4,223
  • 9
  • 46
  • 77
  • 5
    The question you should be asking is why you are even contemplating writing code like this. Do you like pain? – David Heffernan Oct 08 '13 at 12:03
  • 1
    I agree with David, once you get a basic understanding, if you have to read documentation/write a test/ask to SO, probably your expression is your complex that either it is very likely to not behave as expected or your coworkers will have to read documentation/write a test/ask to SO to understand it (or both of the options). Lines of code are free, just make your expression longer. – SJuan76 Oct 08 '13 at 12:11
  • **Note**: Expression `(--(*p))++` (is equivalents to `--(*p)++`) will be a **compilation time error**: `Not an lvalue`. To understand it Read: [Why `++i++` gives “L-value required error” in C?](http://stackoverflow.com/questions/17850851/why-i-gives-l-value-required-error-in-c/17850934#17850934) – Grijesh Chauhan Oct 08 '13 at 19:37

4 Answers4

4

The expression:

j = --*p++;

is equivalent to:

j = --*p;  // first decrements value pointed by p, then assign value pointer by p to j
p++;       // increment p to point to next location

The parenthesis version of your expression should be : --(*(p++)); Read @Jonathan Leffler's answer.

I would also suggest you to read @Eric Lippert answer: Incrementing Pointers, Exact Sequence to understand ++ and * operator expression that how a compiler can perform it at low level.

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
2

I am confused by the operator precedence table give in http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm. What does right-to-left and left-to-right mean in this table?

Precedence and associativity determine how the parentheses are logically inserted into an underparenthesized expression. If you have

x + y * z

then * is higher precedence, so it wins, and this is:

x + (y * z)

not

(x + y) * z

If you have two operators that have the same precedence then which one wins depends on the associativity. + and - are the same precedence and have left-to-right associativity, so

x + y - z 

is

(x + y) - z

And not

x + (y - z)

Operators with right-to-left associativity put the parentheses on the rightmost expression first.

I want to know in what order the operator will we applied on this code. --*p++

Well, follow the chart. We have *, prefix decrement and postfix increment. Consult the precedence table first. Postfix increment is higher precedence than the other two, so automatically this is --*(p++). And now we do not need to consult the table to work out the rest; clearly the only possible parenthesization is --(*(p++)).

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • *`Precedence and associativity determine how the parentheses are logically inserted into an underparenthesized expression`* opposite is also true that "parentheses are used to overwrite precedence of operator in an expression". e.g. in `(x + y) * z`, `+` that is low in precedence executes before then `*`. – Grijesh Chauhan Oct 09 '13 at 04:25
1

++ and -- Prefix increment/decrement right-to-left and later on you found *

j = --*p++;  

Above statement according to precedence -- will perform first. So in expression -- is pre-decrement on *p and then ++ is post increment, will increment pointer p.

j= --*p; //and immediately do *p++ == p++ , because here ++ got priority. and this is post decrement this wont assign to j.this as same as incrementing p value in next statement. 
p++;
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Gangadhar
  • 10,248
  • 3
  • 31
  • 50
0

There are plenty of tricks on prefix increment and postfix increment.
As a programmer, you do not need to spend too much time on those tricks, just use brackets in your code to show the computing order.

CobbLiu
  • 447
  • 1
  • 7
  • 10