*ptr++ and ++*ptr are they representing the same expression if not then how they are different according to precedence ?

Recommended Answers

All 7 Replies

I refuse to answer, on the grounds that you should be enthusiastically TESTING this yourself. ;)

Put down your latte, take 30 seconds, and test it, FCOLoud!

i have already checked that but can you tell me how ++*ptr is increment to a pointer

No, I can't tell you, because on my compiler, if:

int n=3;
int *ptr;
ptr = &n;
++*ptr;
printf("\n%d", *ptr);

prints out: 4

So ++*ptr is the same as ++(*ptr).

*ptr++, on the other hand, is the same as *(++ptr) - meaning it does increment the pointer address on my compiler, so the output is usually 0, but conceptually, could be any integer.

When you have an expression to be evaluated, and you're not sure of the precedence for it, explicitly set it up with the right parenthesis's.

++ operator has higher preference than *
so *ptr++ is essentially *(ptr++)
and *++ptr means *(++ptr)
and ++*ptr means ++(*ptr)

His question referred to ++*ptr, not *++ptr.

No, I can't tell you, because on my compiler, if:

int n=3;
int *ptr;
ptr = &n;
++*ptr;
printf("\n%d", *ptr);

prints out: 4

So ++*ptr is the same as ++(*ptr).

*ptr++, on the other hand, is the same as *(++ptr) - meaning it does increment the pointer address on my compiler, so the output is usually 0, but conceptually, could be any integer.

When you have an expression to be evaluated, and you're not sure of the precedence for it, explicitly set it up with the right parenthesis's.

but i am getting different answer on my compiler
according to that
++*ptr is increment to pointer
and *ptr++ is increment to value pointed by ptr
can anybody explain

Other way around, Daredevil. ++*ptr will increment the value that *ptr points to, not the pointer, itself.

And *ptr++ is an increment to the ptr address.

so *ptr++ is *(ptr++) incrementing the ptr address
and ++*ptr means ++(*ptr) and increments the value that *ptr points to.

How does that compare to what you're getting?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.