Re: question about pointer
From: Kevin Goodsell (usenet1.spamfree.fusion_at_neverbox.com)
Date: 12/17/03
- Next message: no spam: "Re: static const?"
- Previous message: Richard Heathfield: "Re: Why isn't this working correctly?"
- In reply to: Bo Sun: "question about pointer"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: no spam: "Re: static const?"
- Previous message: Richard Heathfield: "Re: Why isn't this working correctly?"
- In reply to: Bo Sun: "question about pointer"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|