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 ?

Recommended Answers

All 8 Replies

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++));
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.

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

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

commented: Little value on it. -2

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);
{

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.