J
junky_fellow
Is *ptr++ equivalent to *(ptr++) ?
Is *ptr++ equivalent to *(ptr++) ?
Is *ptr++ equivalent to *(ptr++) ?
Richard Heathfield said:Yes. In each case, the associativity is right-to-left and, in each case, the
value seen by * is the old value of ptr, not the new value that it will
have when ++ has completed its work.
yes it is correct....
one more
++*ptr == (*ptr)++
Chris said:I hardly think so.
Vijay said:The two constructs are equivalent in the following scenarios:
int *ptr = ...;
int i;
(void)++*ptr;
(void)(*ptr)++;
However, their behaviour is not the same when used like this:
i = (*ptr)++;
i = ++*ptr;
Chris said:[..]
Hence, they are not equivalent; you can't just use one in place
of the other.
Vijay said:[..]
Yes, you're quite correct. But for a learner, he/she must know the
difference.
Practically, these two construct should not be interchanged.
S.Tobias said:I don't think "associativity" is the right word here, as they are
unary operators. "Precedence" is more appropriate, I think.
But the precedence of *(indirection) and ++(post increment) operators
is same.
So, I think associativity should be the right word.
S.Tobias said:I don't think "associativity" is the right word here, as they are
unary operators. "Precedence" is more appropriate, I think.
S.Tobias said:
unary-expr:
postfix-expr
unary-operator cast-expr
unary-operator: one of
& * + - ~ !
cast-expr:
unary-expr
( type-name ) cast-expr
Richard Heathfield said:Wrong. They are the same.
That isn't particularly helpful. Of rather more explanatory power is the
table on p53 of K&R, which shows us that * and ++ have the same precedence
level, so the ordering within the statement becomes relevant - and that
ordering is right-to-left for that particular precedence level.
Richard Heathfield said:That isn't particularly helpful. Of rather more explanatory power is the
table on p53 of K&R, which shows us that * and ++ have the same precedence
level, so the ordering within the statement becomes relevant - and that
ordering is right-to-left for that particular precedence level.
The precedences shown in that table are not the same as the precedences
implied by the grammar in the standard. It's of no practical
consequence since, as you might expect given the source, the table
produces the same result for all valid expressions, but it does have an
impact on academic discussions such as this one. According to the
standard, postfix ++ and -- do have higher precedence than unary *
(which has the same precedence as prefix ++ and --; K&R's table give the
prefix and postfix operators the same precedence and count on
associativity to get the correct results).
Chris said:I hardly think so.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.