473,729 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1703


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
2388
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: An expression a = b() + c() * d++; can be transformed with the rules of operator associativity and operator precedence into a tree
21
3037
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 opposed to what is mentioned in precedence table? Also, with comma operator. consider,
7
1788
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 -------- ------------- ! ~ ++ -- - (type) * & sizeof right to left Is there a complete table on operators, their precedence and
5
2540
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
3746
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'); printf("%d", a); /* code end */ 2
5
2116
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 means *(p++)? I think I am wrong. (so, associativity is not for operand?) But for *++p, right to left associativity means *(++p) is correct. So by definition of associativity, I guess it only applys on grouped
25
1577
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 general rule as to when this type of increment occurs? ( I understand that the increment in "c = ++*s" occurs immediately before assignment, so my guess is the same
32
3315
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 += i + j + k++;
31
1796
by: Jim Langston | last post by:
In Python 2.5 on intel, the statement 2**2**2**2**2 evaluates to 20035299304068464649790723515602557504478254755697514192650169737108940595563114 53089506130880933348101038234342907263181822949382118812668869506364761547029165 04187191635158796634721944293092798208430910485599057015931895963952486337236720 30029169695921561087649488892540908059114570376752085002066715637023661263597471...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.