473,320 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

question regarding precedence and associativity of ++ and *

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
3 1687


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
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
thanks ....it definitely helped.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sensorflo | last post by:
After browsing though many newsgroups articels I'm still not shure how operator precedence, operator associativity, sequence points, side effects go together. Currently I have the following view: ...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
7
by: lovecreatesbea... | last post by:
c-faq, 4.3: The postfix ++ and -- operators essentially have higher precedence than the prefix unary operators. BSD Manual, OPERATOR(7): Operator Associativity...
5
by: junky_fellow | last post by:
Hi, I have a very basic doubt about associativity and precedence of operators. I am not a computer science person and may find it quite weird. Consider an expression 4+5*2
9
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
5
by: fdmfdmfdm | last post by:
Associativity in C takes two forms: left to right and right to left. I think the K&R book lacks something... For example, *p++, since the associativity is from right to left, do this expression...
25
by: mdh | last post by:
Given the expression: while (isaspace(c = *s++)) x+1; (s is an array) Does the increment to 's' occur after "x+1" is evaluated, or after the content of s is assigned to c? Is there a...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
31
by: Jim Langston | last post by:
In Python 2.5 on intel, the statement 2**2**2**2**2 evaluates to 20035299304068464649790723515602557504478254755697514192650169737108940595563114...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.