Re: question regarding precedence and associativity of ++ and *
- From: Richard Heathfield <invalid@xxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 28 Jul 2005 07:16:13 +0000 (UTC)
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
.
- References:
- Prev by Date: Re: gcc 4 signed vs unsigned char
- Next by Date: Re: Paasing global variables to functions
- Previous by thread: Re: question regarding precedence and associativity of ++ and *
- Index(es):
Relevant Pages
|