Register to reply

Question about pointers in C.

by MathematicalPhysicist
Tags: pointers
Share this thread:
MathematicalPhysicist
#1
Jul5-07, 08:33 AM
P: 3,214
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.
Phys.Org News Partner Science news on Phys.org
Suddenly, the sun is eerily quiet: Where did the sunspots go?
'Moral victories' might spare you from losing again
Mammoth and mastodon behavior was less roam, more stay at home
neurocomp2003
#2
Jul5-07, 09:01 AM
P: 1,373
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.
K.J.Healey
#3
Jul5-07, 09:30 AM
P: 641
Yes. Since P is a memory address as a pointer, heres 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.

D H
#4
Jul5-07, 09:42 AM
Mentor
P: 15,053
Question about pointers in C.

Quote Quote by Healey01 View Post
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 demon fly out of the programmer's nose.
K.J.Healey
#5
Jul5-07, 10:39 AM
P: 641
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.
MathematicalPhysicist
#6
Jul5-07, 11:03 AM
P: 3,214
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?
D H
#7
Jul5-07, 11:19 AM
Mentor
P: 15,053
Quote Quote by Healey01 View Post
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.
Jimmy Snyder
#8
Jul5-07, 11:27 AM
P: 2,179
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.
neurocomp2003
#9
Jul5-07, 11:56 AM
P: 1,373
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 thats 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.
MathematicalPhysicist
#10
Jul5-07, 12:32 PM
P: 3,214
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 im not sure, this is why asked if there's a difference.

p.s
im learning from colourful lecture notes. (-:
KTC
#11
Jul5-07, 12:42 PM
P: 90
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!
KTC
#12
Jul5-07, 12:44 PM
P: 90
Quote Quote by loop quantum gravity View Post
int *ptr,ii,jj;
Bad, bad, bad.

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

int *ptr, *ii, *jj;
Or use separate lines when you're declaring pointers to avoid such errors!
Mr Virtual
#13
Jul5-07, 01:23 PM
P: 218
Hi loop q g

naively i think that zero, but im 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
rcgldr
#14
Jul5-07, 04:11 PM
HW Helper
P: 7,030
++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;
KTC
#15
Jul5-07, 05:26 PM
P: 90
Quote Quote by Mr Virtual View Post
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 this page for what is actually guarantee by the language in terms of the size of the integer types.
Mr Virtual
#16
Jul5-07, 07:09 PM
P: 218
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
rcgldr
#17
Jul5-07, 10:41 PM
HW Helper
P: 7,030
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[i];
PTR2PORT1234[i] = value;
mattst88
#18
Oct13-07, 08:50 PM
P: 29
Quote Quote by neurocomp2003 View Post
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.


Register to reply

Related Discussions
C Pointers explained! Programming & Computer Science 4
Pointers and arrays question. Programming & Computer Science 3
Coding a Bubble Sort routine using a pointer array Programming & Computer Science 4
New and looking for pointers. General Discussion 0
Need some pointers Introductory Physics Homework 21