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

  • C/C++ Ambiguity in Order of Evaluation
    ... I have never been able to resolve the issue of order of evaluation in ... precedence and grouping of operators." ... That unary - has the highest precedence or the use of parentheses in - ... about binding operands to operators. ...
    (comp.lang.c)
  • Re: C/C++ Ambiguity in Order of Evaluation
    ... I have never been able to resolve the issue of order of evaluation in ... C/C++ and the related issue of precedence of operators, ... precedence and grouping of operators." ... That unary - has the highest precedence or the use of parentheses in - ...
    (comp.lang.c)
  • Re: Remebering the order of precedence
    ... Do you mean to convey that parentheses can be used to ... But, unless we know the order of precedence of the operators, ... Do you mean to convey that we can use parentheses to simplify ... you can force whatever evaluation you like by ...
    (comp.lang.c)
  • Re: C/C++ Ambiguity in Order of Evaluation
    ... I have never been able to resolve the issue of order of evaluation in ... C/C++ and the related issue of precedence of operators, ... precedence and grouping of operators." ... That unary - has the highest precedence or the use of parentheses in - ...
    (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)