``````int a[10]={3,45,6,78,89,334,4,77,54,60};
int *p=a;``````

what will *p++ do ?

as ++ has more priority than * therefore ++ will be executed fisrt ?
but its postincrement so it will happen after change of line or statement.

so will it increment the pointer or increment the value hold by pointer ?

*++p will increment the pointer and then derefrence it !! right ?
*(p++) will do the same ?
++(*p) will increment value !!

now can i achieve increment in both value and pointer in one line ?

9
Contributors
8
Replies
11
Views
7 Years
Discussion Span
Last Post by jaaz....
``````int a[10]={3,45,6,78,89,334,4,77,54,60};
int *p=a;``````

what will *p++ do ?

as ++ has more priority than * therefore ++ will be executed fisrt ?
but its postincrement so it will happen after change of line or statement.

so will it increment the pointer or increment the value hold by pointer ?

*++p will increment the pointer and then derefrence it !! right ?
*(p++) will do the same ?
++(*p) will increment value !!

A quick test program will tell you exactly what each of these do.

now can i achieve increment in both value and pointer in one line ?

For readability, why would you want to? The more you combine tasks the more confusing the code becomes.

now can i achieve increment in both value and pointer in one line ?

It's pretty straightforward -

``````// increments pointer and then value
++(*(++p));
// increments value and then pointer
++(*(p++));``````

Edited by 0x69: n/a

``int v = *p++``

p gets dereferenced first and then incremented (post-increment). So after the assignment the value of v will be 3 and p will be pointing to 45.

``int v = *++p``

here p first gets incremented and then dereferenced (pre-increment). So after the assignment the value of v will be 45 and p will also be pointing to 45.

A simple rule of thumb is finish up all the expression to the left, then come and evaluate the expression to the right of the pointer

For examples:
v= *p++
Go left and de reference the pointer then come and increment the pointer

A simple rule of thumb is finish up all the expression to the left, then come and evaluate the expression to the right of the pointer

I just follow the operator precedence rules.

Yes why follow rules of thumb when you can in fact just follow THE rules :D

Yes you can do this by

(*(++p -1))++;

it will do both

increments the value at very first location
moves the pointer p to point the next location

Little value on it.

Yes you can do this by

(*(++p -1))++;

it will do both

increments the value at very first location
moves the pointer p to point the next location

Why are we indulging this thread? What are we trying to teach or convey?
Especially, when anyone with an inclination to truly learn the language would have started any text editor and written a couple statements, compiled them, and observed the result.

what is output of following code?
main()
{
char *p="hai friends",p1;
p1=p;
while(p!='\0') ++*p++;
printf("%s %s",p,p1);
{

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.