What is meaning of (++*ptr) in Pointer ?

January 8, 2012 No Comments » Hits : 198





Meaning of (++*ptr) in C Programming :

Consider the Following Example : -

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

Explanation :

  1. ‘++’ and ‘*’ both have Equal Precedence
  2. Associativity is from Right to Left ( Expression evaluated from R->L)
  3. Both are Unary (Operates on single operand )
  4. So ‘ * ‘ is Performed First then ‘ ++ ‘


Calculation of Answer :

  ++*ptr     =  ++ ( *ptr )
             =  ++ ( *3058 )
             =  ++ ( 20 )
             =  21
  1. Suppose ptr variable stores address 3058.
  2. * Operator will de-reference pointer to get value stored at that address. (i.e 20)
  3. Pre-increment on 20 will perform increment first then assignment

Live Example :

#include<stdio.h>

int main()
{
int n = 20 , *ptr ;
ptr = &n;
printf("%d",++*ptr);

return(0);
}

Output :

21

Incoming search terms: