473,396 Members | 1,809 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,396 software developers and data experts.

Pointer math

Hi group!

I have here five different declarations but I have some problems to
understand this concept. I know there are more examples if
I would use parentheses but I think the following ones are common.

What I've learned..

int myArray[3] = { 0, 1, 2 };
int *ptr = myArray;

1. *ptr++
2. *++ptr
3. ++*ptr
4. ++*++ptr
5. ++*ptr++

1. printf("%d",*ptr++);

result: 0 first array element

because the postfix increment operator has a higher precedence
than the dereferencing operator. Associativity LEFT to RIGHT. That
would mean the same as *(ptr++) First
incrementing the pointer then the OLDER value will be fetched.

But why the older value? The incrementing process was the first
action?

2. printf("%d",*++ptr);

result: 1 second array element

The dereferencing and the prefix operators have the same
precedence. Associativity RIGHT To LEFT.That
would mean *(++ptr) First incrementing the pointer then fetching
the value.

3. printf("%d",++*ptr)

result: 1 second array element

The dereferencing and the prefix operators have the same
precedence. Associativity RIGHT to LEFT. The
would mean++(*ptr). First fetching the value THEN incrementing.

But why was the result 1 and not 0?

4. printf("%d",++*++ptr)

result: 2 the third array element

The dereferencing and the prefix operators have the same
precedence. Associativity RIGHT to LEFT. That would mean first
incrementing the pointer THEN fetching the value THEN incrementing
that value?

5. printf("%d",++*ptr++);

result: 1 the second array element

The postfix operator has a higher precedence than the prefix and
dereferencing operator. That would mean
the same as ++*(ptr++). First the pointer will be increment then
fetching the value and then incrementing that
value. Here the Associativity is tricky.

I hope I'll get some clarity.

Thanks for any help!
Tom
Nov 11 '08 #1
3 7387
On Nov 11, 3:52*pm, tfelb <tomico...@gmail.comwrote:
Hi group!

I have here five different declarations but I have some problems to
understand this concept. I know there are more examples if
I would use parentheses but I think the following ones are common.

What I've learned..

int myArray[3] = { 0, 1, 2 };
int *ptr = myArray;

1. *ptr++
2. *++ptr
3. ++*ptr
4. ++*++ptr
5. ++*ptr++

1. printf("%d",*ptr++);
Ok, this is gets the value from ptr - it's 0, because after
int *ptr = myArray;
it points to the zeroth element. So printf prints 0, and the this
(local!) value is incremented.
>
2. printf("%d",*++ptr);
That's clear. ptr points to the zeroth element. After increment it
points to the first elemnt, so it's value is 1.
3. printf("%d",++*ptr)
That's easy too: ptr points to the zeroth element, you get it's value
and increments in, so you get 0 + 1 == 1.
4. printf("%d",++*++ptr)
After ++ptr ptr points to the first element of the array, you get its
value and increments it, so you get 2.
5. printf("%d",++*ptr++);
Here you get value of the ptr that points to the zeroth element,
increments it (prefix ++), the printf prints it (of course it's 1),
then the local value increments again (postfix ++).
Nov 11 '08 #2
tfelb wrote:
>
int myArray[3] = { 0, 1, 2 };
int *ptr = myArray;

1. *ptr++
2. *++ptr
3. ++*ptr
4. ++*++ptr
5. ++*ptr++

1. printf("%d",*ptr++);

result: 0 first array element
Yes.
because the postfix increment operator has a higher precedence
than the dereferencing operator.
Yes.
Associativity LEFT to RIGHT.
Unary operators don't have any "associativity".
That
would mean the same as *(ptr++)
Yes.
First incrementing the pointer then the OLDER value will be fetched.
"First"? No. Neither operator precedence nor parentheses impose any
temporal ordering on the operator execution process. No one knows what
happens "first" or "next". The only thing that we know here is that
'ptr++' evaluates to the original value of 'ptr', and that by the next
sequence point the value of 'ptr' will get updated.
But why the older value?
Because that's what the specification of the postfix increment says.
Postfix increment always returns the original ("older") value of its
operand.
The incrementing process was the first action?
There's no such thing as "first action" here. Regardless of what happens
and when, postfix increment always returns the original value - that's
all you really need to know in this case.
2. printf("%d",*++ptr);

result: 1 second array element
Yes.
The dereferencing and the prefix operators have the same
precedence.
Er... No. There's no such thing as "precedence" for unary operators
specified on the same side of the operand. Both '*' and prefix '++' are
specified on the left. The one that "closer" to the operand ('ptr')
applies to the operand, the one that's further from the operand applies
to the result of the previous operator.
Associativity RIGHT To LEFT.
No. Again, there's no such thing as "associativity" with unary operators.
That would mean *(++ptr)
Yes. Again, simply because '++' is "closer" to 'ptr' than '*'.
First incrementing the pointer then fetching
the value.
Wrong again. It means that '*' is applied to the incremented value of
'ptr' (i.e. to 'ptr + 1'). When the value of 'ptr' will actually change
- no one knows. It will change by the next sequence point, that's for
sure. The compiler is free to evaluate it as follows

1. read *(ptr + 1)
2. change the value of 'ptr' to 'ptr + 1'

As you can see, the actuall incrementing of the pointer is the last (not
first) step in this case.
3. printf("%d",++*ptr)

result: 1 second array element
No. The result is the new value of the _first_ array element.
The dereferencing and the prefix operators have the same
precedence. Associativity RIGHT to LEFT.
Both statement are wrong/irrelevant. See above.
The
would mean++(*ptr). First fetching the value THEN incrementing.
Same mistakes as before. What it really means is that '++' is applied to
the result of '*ptr'. Since this is the prefix '++', it is guaranteed
that it evaluates to the new value of the operand.
But why was the result 1 and not 0?
Because the new value is 1.
4. printf("%d",++*++ptr)

result: 2 the third array element
Again, no. The result is the new value of the _second_ array element.
The dereferencing and the prefix operators have the same
precedence. Associativity RIGHT to LEFT. That would mean first
incrementing the pointer THEN fetching the value THEN incrementing
that value?
Same mistakes. It means: calculate the new value of 'ptr', dereference
it, calculate the new value of that memory location; also update 'ptr'
and that memory location before the next sequence point.
5. printf("%d",++*ptr++);

result: 1 the second array element
No. The result is the new value of the _first_ array element.

All the same mistakes as above.

--
Best regards,
Andrey Tarasevich
Nov 11 '08 #3
In article <87************@bsb.me.uk>, Ben Bacarisse <be********@bsb.me.ukwrote:
>Another way to look at it, is simply that ++x has the value x + 1 with
the side effect of incrementing x by the next sequence point. x++ has
the value x with the same side effect.
Very clearly and succinctly put. Thank you.
Nov 12 '08 #4

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

Similar topics

7
by: Frank | last post by:
Sorry that the question I posted few minutes ago didn't correctly describe the problem. So please ignore it. I repost the question as below: ============================================== I...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
26
by: Meenu | last post by:
Hi, Can two different far pointers contain two different addresses but refer to the same location in memory?
3
by: TomHL | last post by:
this code set the color that I want only on the first pixel. I have an X & Y values of the pixel that I want to change. How do I change the pointer's position according to my X & Y values that I...
4
by: flopbucket | last post by:
Hi, Quick question regarding pointer math. I have a simple container class - something like std::vector<> - that I am implementing. My iterators are implemented as pointers: template<class...
16
by: quantumred | last post by:
Can I use pointer arithmetic on the members of a structure in the following way? Should I be worried about structure padding? This works in my debugger but I wonder if I'm bending some rule here. ...
6
by: reji_thomas | last post by:
Hi, I have a doubt in the following code: struct xyz { int x; long l; float f; };
8
by: conrad | last post by:
given: char *p = NULL + 1; Does the above expression produce undefined behavior? Where would the section be in the standard that describes this? -- conrad
4
by: Ryan Knopp | last post by:
I'm having some bizarre problems with some of my memory allocation. I've ran it threw gdb and noticing my memory indexes aren't matching up. here's an example. I have commented within the code....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.