top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C : Difference between ++*p, *++p and *p++ in case pointer p is pointing to an integer array ?

+1 vote
1,170 views

int arr[ ] = { 1, 2 };
p = arr; /* p is pointing to arr */

How pointer p will behave ?

posted Oct 5, 2016 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+3 votes

If you go through precedence table, then post increment/decrement has higher precedence than pre increment/decrement.

Bonus Article : - Why Post increment is higher precedence than pre increment [ http://bit.ly/2dLll0F ]

Precedence 2 - post increment/decrement and Associativity - Left to Right
Precedence 3 - pre increment/decrement and Associativity - Right to left

  • has same precedence with pre increment/decrement, and associativity is Right to left.

Expression 1:
++*p - Here as pre increment and * is there and both has same priority so it executed as Right to left : so first *p and then ++
So according to ur question *p = 1, ++(*p) so which expands to *p = (*p) + 1, so *p becomes 2, arr[0] becomes 2;

Expression 2:
*++p - Here as pre increment and * is there and both has same priority so it executed as Right to left : so first ++p and then *
So according to ur question ++p ( p = p + (1* size of (type of data it is pointing) here int), now p is pointing to the next element of the array. so *p prints the value of next element of array which is 2;

Expression 3:
*p++ - Here as post increment is higher precedence than * so p++ ( p = p + (1* size of (type of data it is pointing) here int)) and then *[new value of p] , P++ pointing to the next element of the array so pointing to 2, *p prints 2.

Hope it is clear

answer Oct 5, 2016 by Sachidananda Sahu
...