Re: question regarding precedence and associativity of ++ and *



maadhuu wrote:

> hi,
> i am a bit confused as to how *i++ (i is a pointer) works.....the postfix
> + has a higher precedence than prefix ++ and i think precedence is from
> left to right whereas ,

The precedence of + is irrelevant. The "maximum munch" principle ensures
that ++ will be recognised.

> prefix ++ and * have same precedence and think its
> from right to left......
> well, can someone plss enlighten me how this works ??

First think about the ++. As you know, i++ has two jobs: one of those jobs
is to increase the value of i by one (or, in the case of a pointer, to
point to the next object along), and the OTHER job is to "return" i's old
value. Now we can think about the *. It is given i's OLD value to work
with, not i's new value. Thus, you would expect this code:

#include <stdio.h>

int main(void)
{
int array[] = { 0, 42 };
int *i = array;

*i++ = 6;
printf("%d\n", array[0]);
printf("%d\n", array[1]);
printf("ptr incremented? %s\n", i == &array[1] ? "Yes" : "No");
return 0;
}

to display

6
42
ptr incremented? Yes



--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
.



Relevant Pages

  • Re: len() should always return something
    ... Some aspects of the Python design are remarkably clever, ... I admit I'm somewhat conflating this principle with the anti-ambiguity ... treats scalars as collections. ... represent an int (obviously breaks down a bit with arbitrary-magnitude ...
    (comp.lang.python)
  • Re: Equation parsing
    ... /*infix operator f where precedence of left & right expression is ... int precedence; ... precedence of the op at the top of the tree you have just parsed and the ... Basically I have a struct called math_term which is something ...
    (comp.lang.c)
  • Re: Equation parsing
    ... /*infix operator f where precedence of left & right expression is ... int precedence; ... of the op at the top of the tree you have just parsed and the op that is ... Basically I have a struct called math_term which is something ...
    (comp.lang.c)
  • Re: Array and binding question.
    ... int *arr(an array of 13 pointers to integers, ... there have been extensive discussions about precedence and ... operators are associated with which operands. ... the effect of the syntax rules by saying things like "binds more ...
    (comp.lang.c)
  • Re: How to parse ?
    ... The precedence and associativity rules of the operators can be taken ... take the content of that int. ... "LISP is worth learning for the profound enlightenment experience ...
    (comp.lang.c)