By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,170 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

question regarding precedence and associativity of ++ and *

P: n/a
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 ,prefix ++ and * have same precedence and think its
from right to left......
well, can someone plss enlighten me how this works ??
thanx.

Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a


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 ,prefix ++ and * have same precedence and think its
from right to left......
well, can someone plss enlighten me how this works ??
thanx.


Have a look at,
http://groups-beta.google.com/group/...65c45ad7a86514

Nov 15 '05 #2

P: n/a
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
Nov 15 '05 #3

P: n/a
thanks ....it definitely helped.

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.