Re: question about pointer

From: Kevin Goodsell (usenet1.spamfree.fusion_at_neverbox.com)
Date: 12/17/03


Date: Wed, 17 Dec 2003 20:47:42 GMT

Bo Sun wrote:

> hi:
>
> please take a look at the following code:
>
> int p[4]={111, 222, 333, 444}, *q = p, p1, p2, p3;
>
> p1 = *q++;
> p2 = *(q++);
> p3 = *(++q);
>
>
> what is value of p2? I think it should be 333. however, the result of p2
> is 222.
>
> what is wrong here?
>
> thanks,
>
> bo

You seem to be making the common mistake of believing that the
parentheses will somehow force complete evaluation of the increment
before anything outside of the parenthesis is considered. In fact,
parentheses do not change order of evaluation, they only override
precedence. Overriding precedence can sometimes have the effect of
changing order of evaluation, but this is not such a case.

Checking a precedence chart, you should be able to determine that in the
expression

*q++

the ++ binds more tightly than the *. You probably also know,
intuitively, that using parens to clarify this fact should not change
the meaning of the expression. The following expression is exactly the
same - the parens are redundant.

*(q++)

And the reason you know this, intuitively, is that you've seen it in
other, less confusing cases. You know these two expressions are equivalent:

a + b * c

a + (b * c)

-Kevin

-- 
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


Relevant Pages

  • Re: Article about Herb Schildt accepted at comp.risks
    ... Do you mean there is no such page, or do you mean that Microsoft's compiler allows the control of evaluation order via parentheses? ... 'Parentheses are operators that increase the precedence of the ... Precedence is about which operands are associated with which operators, whereas order of evaluation is all about the order in which things are evaluated. ...
    (comp.lang.c)
  • Re: Article about Herb Schildt accepted at comp.risks
    ... 'Parentheses are operators that increase the precedence of the ... changing order of execution as necessary as possible. ... evaluation" and "precedence", because that phrase and that word merely ...
    (comp.lang.c)
  • Re: Article about Herb Schildt accepted at comp.risks
    ... entitled to change the order of evaluation if the precedence is ... implementation is entitled to use any order of evaluation in likes. ... better way to say it is that parentheses themselves have very high ... that most American programmers simply do not code much. ...
    (comp.lang.c)
  • Re: Article about Herb Schildt accepted at comp.risks
    ... entitled to change the order of evaluation if the precedence is ... implementation is entitled to use any order of evaluation in likes. ... better way to say it is that parentheses themselves have very high ... that most American programmers simply do not code much. ...
    (comp.lang.c)
  • Re: operator precedence in Fortran 2008 draft
    ... to take operands of type BITS. ... to overload the relational operators as well, ... the operator precedence chosen by standard ... forces the use of parentheses in the common cases. ...
    (comp.lang.fortran)