What is the difference between ++p and p++ in C pointers?

  • Thread starter MathematicalPhysicist
  • Start date
  • Tags
    Pointers
In summary: What do you mean by "such erroneous conversions"? The code you posted is perfectly fine in both C and C++, and definitely won't crash.Just to clarify, in this case, ptr is pointing to an int, not an array. If ptr were pointing to the first element of an array, then the statement would be incorrect in C++ (but fine in C, as you say). However, in either case, it's not a particularly elegant or clear way of writing code.In summary, when using increment operators with pointers, ++p will increment the pointer and then perform the operation you ask it, while p++ will increment the pointer after performing the operation you ask it. If the difference between the two operators matters, it is important
  • #1
MathematicalPhysicist
Gold Member
4,699
371
if i have declared int *p
and afterwards, in the progress of the programme iv'e typed
++p or p++
is there any difference between and what exactly is the difference?

thanks advance.
 
Technology news on Phys.org
  • #2
yes postfix and prefix...i can never remmeber if the post/pre imply the ++ or p

++p will increment the pointer and then perform the operation you ask it

and
p++ will increment the pointer after performing the opreation you ask it

you can always try
x=++p; printf("...",*x); printf("%p",x);
x=p++; printf("...",*x); printf("%p",x);

in C: printf("%p",x); is your friend unless you know how to use a debugging program.
 
  • #3
Yes. Since P is a memory address as a pointer, here's why:

If you do something to P, say :
P=5;
P = (P++)*5;
Then P will be equal to 25.

If you do
P=5;
P = (++P)*5;

It calculates ++P BEFORE the operations are performed, and that P is incremented.
So you should get P=30;

Either way they both permanently change P (until you change it back). Its a matter of whenter its ++before or after++ the operation line.

"--" works the same way.
 
  • #4
Healey01 said:
If you do something to P, say :
P=5;
P = (P++)*5;
Then P will be equal to 25.

It could also be 20, 30, 1024, or any random value. What you just posted is a classical example of an undefined construct in C. This code could even make a http://catb.org/~esr/jargon/html/N/nasal-demons.html" .
 
Last edited by a moderator:
  • #5
Hmm, why? I'm not a super programmer.

Ahh, I see.
Yeah, forgot i was working with a pointer there for a moment.

Same idea, but P++ will increment the pointer to point to the next memory address.
 
  • #6
so if p is in cell 1200, and
p=&x;
for int x=20;
then int y=++p;
is y==1201, and int y=p++; is y==21, or something else?
 
  • #7
Healey01 said:
Yeah, forgot i was working with a pointer there for a moment.
Forget the pointer business for a while. Your little code snippets such as
P = (P++)*5;
invoke unspecified behavior because both the = and ++ operators alter the value of P. Which one should the compiler use? The language specification doesn't say which. The language specification says that this kind of statement is illegal, and illegal in a very nasty way. The compiler can do ANYTHING with statements that invoke unspecified behavior, including nothing at all or wiping your disk drive, and still be in full compliance with the standard.
 
  • #8
If the difference between ++p and p++ matters, then you must use the appropriate one. However, in those cases where it does not matter, I always use ++p. The reason is that I got into the habit back in the day when there was a performance gain by doing so. Compilers used to dereference a variable twice (for instance p [x[k]][y[k]] takes a lot of dereferencing) for post-increment, but only once for preincrement. With optimizing compilers, the issue has gone away, but the habit remains. Also, someone has noted that ++p reads increment p which sounds better than p++ which reads p increment.
 
  • #9
lqg: are u using a textbook for a class? or learning on your own?

the equations you are giving us are operations acting on both (int) and (int*).
Or in generic terms (datatype) and (datatype*)

As stated in someone's post above this will produce erroneous data...

if p is a ptr: it will always produce an address so in your example 1200 or incremented. Go back to your own example and reread it as that..."p is always a ptr" and try to figure out p++ and ++p where "p is a ptr". To dereference(hope that's the right term) a ptr you use the * (eg *p) and this gives you the datatype of the ptr.

when you implement code in C...your operations should be acting on the same datatype (where ptr is also a datatype) similar to the concepts of operators in algebra

since ++ is unary...it takes a datatype and returns the same datatype.

if you try y=p++ you are acting on 2 different datatypes. Correct that statement as suggested above and then you should be able to figure out the postfix/prefix...Try the example i posted in my first thread where x,p are ptrs to the same datatype and the ... represents the datatype you chosen for printf.
 
  • #10
what about the next thing
int *ptr,ii,jj;
int arrayInts[]={4,6,8,9,10,12,14,15,16,18};
ptr=arrayInts;
ii=++ptr; jj=ptr++;
printf("%d", ii-jj);
what will be printed if the first place of the array is in 1200.

naively i think that zero, but I am not sure, this is why asked if there's a difference.

p.s
im learning from colourful lecture notes. (-:
 
  • #11
Just for those that might be asking or wondering.

An increment (++p OR p++) increment the address pointed to by p by sizeof( type pointed to by p ) amount. So ++c where c is a char* and c points to say 1234 beforehand, afterwards will point to 1235, whereas ++i where i is an int* will cause i to point to an address of at least 1236. (*at least* because C doesn't guarantee the size of the integer types (apart from char), so it could be 1237, or 1238, or ... etc.) But then, you shouldn't be worrying about the actual physical address a pointer is pointing to!
 
  • #12
loop quantum gravity said:
int *ptr,ii,jj;
Bad, bad, bad.

ptr is a pointer to an int. ii and jj is an int, not a pointer!

Code:
int *ptr, *ii, *jj;
Or use separate lines when you're declaring pointers to avoid such errors!
 
  • #13
Hi loop q g

naively i think that zero, but I am not sure, this is why asked if there's a difference.
Yeah, you are right. You will get zero.
When you write,
ii=++ptr;
It means ii=1200+2=1202

But when you write
jj=ptr++;
It means jj=1202
Only after jj has been assigned the value 1202, does the value of p increment to 1204.

So, ii-jj=1202-1202=0.
That's what will be printed on the screen.


Bad, bad, bad.

ptr is a pointer to an int. ii and jj is an int, not a pointer!
You know, when I read the expression,
ii=++ptr;
I thought "what the heck is going on?". I write in C++, which considers such an expression as an error: Error: cannot convert from *ptr to ptr.
But when I tried it in C, I was surprised to see that it worked perfectly. So, I think that C-programers will just have to take it in their stride. But such erronous conversions can be quite harmful for your system (it can crash!).
I think this is one of the shortcomings of C.

Mr V
 
Last edited:
  • #14
++p or p++ ... is there any difference between and what exactly is the difference?
With ++p, p and the pointer value used are incremented before being used. With p++, the existing value of p is used, and after being used, p is incremented. This is a throwback to machine specific instruction sets, which included pre and post incrementing and decrementing, and the authors of C decided to implement this feature into the C language.

In many machine languages, stack "pushes" are implemented as "*(--p) = value", and stack "pops" are implemented as "value = *(p++)".

Getting back on topic.

Given:
int array[5] = {1, 2, 3, 4, 5}l
int *p = array;
int i;

The statement:
i = *(++p);

Is the equivalent of:

p += 1;
i = *(p);

The statement:
i = *(p++);

Is the equivalent of:

i = *(p);
p += 1;
 
  • #15
Mr Virtual said:
Yeah, you are right. You will get zero.
When you write,
ii=++ptr;
It means ii=1200+2=1202
Please see my first post. That statement is not guarantee to be true (even if we assume ii to be of type int*). See http://www.jk-technology.com/c/inttypes.html" for what is actually guarantee by the language in terms of the size of the integer types.
 
Last edited by a moderator:
  • #16
Er.. I was not guaranteeing anything. Just before posting the reply, I checked it all on my TC compiler, and the program ran perfectly.

You are right about integer sizes. For example, Dev C/C++ uses 4 bytes, TC uses 2 bytes, VC++ uses 4 bytes again. And not only integers, even pointers are provided 4 bytes in modern compilers.

You are also right that we cannot convert a pointer into an integer, or vice versa, simply because a pointer stores data in quite a different form than an integer, and we are forcing the compiler to implicitly perform conversion from one data type to another.
For example, if we write,
int i, *ptr; ..1
i=ptr; ..2
ptr=i; ..3


In line 2, I am forcing the compiler to convert data stored in pointer-form, into data which can be stored in an integer. The compiler may be successful in doing so at some times, but may fail miserably on many occasions, leading to wrong results or system failure.

Didn't I mention in my post that C should actually forbid this type of conversion. I am totally against such faulty techniques. But lqg was asking a question, and I told him what the right answer would be, if everything went right inside the computer.

warm regards
Mr V
 
  • #17
integers and pointers
This is getting a bit off topic, but in enviroments with memory mapped I/O or similar situations, declaring pointers to fixed locations in a processors memory map requires type casting of an integer to a pointer, for example on a 32 bit machine where usigned longs and pointer are both 32 bits:

#define PTR2PORT1234 = ((volatile unsigned char*)0xFFFF1234ul)

which allows this usage:

value = *PTR2PORT1234;
*PTR2PORT1234 = value;
value = PTR2PORT1234;
PTR2PORT1234 = value;
 
  • #18
neurocomp2003 said:
yes postfix and prefix...i can never remmeber if the post/pre imply the ++ or p

++p will increment the pointer and then perform the operation you ask it

and
p++ will increment the pointer after performing the opreation you ask it

you can always try
x=++p; printf("...",*x); printf("%p",x);
x=p++; printf("...",*x); printf("%p",x);

in C: printf("%p",x); is your friend unless you know how to use a debugging program.

Those are the exact same since the ++ is in a completely different expression from the printf call.
 

1. What is the difference between ++p and p++ in C pointers?

The main difference between ++p and p++ in C pointers is the order in which the increment operation is performed. When using ++p, the pointer is first incremented and then the new value is used. On the other hand, when using p++, the current value of the pointer is used and then it is incremented.

2. Can the use of ++p and p++ affect the memory address of the pointer?

Yes, both ++p and p++ can affect the memory address of the pointer. The difference lies in where the increment operation is performed. With ++p, the address is incremented before being used, while with p++, the address is incremented after being used.

3. How does the order of operations affect the outcome of using ++p and p++?

The order of operations can affect the outcome of using ++p and p++ because it determines when the increment operation will be performed. If the increment operation is performed before using the pointer, the outcome will be different from when the increment operation is performed after using the pointer.

4. Are there any performance differences between ++p and p++?

No, there are no significant performance differences between ++p and p++. Both operators have the same functionality and the difference lies in the order of operations, which does not affect performance.

5. In what situations would it be recommended to use ++p and p++?

It is recommended to use ++p when you want to increment the pointer first and then use the new value, and to use p++ when you want to use the current value of the pointer and then increment it. The choice between the two operators depends on the specific use case and preference of the programmer.

Similar threads

  • Programming and Computer Science
Replies
3
Views
724
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
878
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
2
Views
930
  • Programming and Computer Science
2
Replies
40
Views
2K
Back
Top