Confusing Pointer Operations : *++ptr, ++*ptr, *ptr++ ( c Interview question - 1)

Important Points:

1. Postfix operation has more precedence than dereference operator.
        *p++ means it will be *(p++).. * will be applied to the result of p++.
2. Prefix Operation has the same precedence with the dereference operator.When there is same precedence we have to take associativity into picture, The associativity is right-left . In ++*ptr , first it will dereference and then increment , whereas in *++ptr it will increment the value and dereference.


To easily understand this we will take an example of a C program.

#include <stdio.h>
int main()
{
       char ptr[]="Hello World";
       printf("Value :%s\n", ptr);
       ++*ptr;
       printf("Value :%s\n", ptr);
       return 0;
}

In the above example you can see that we are having prefix operator and dereference operator.We know that both are having the same precedence, then we will look at the associativity which is  right to left so first it gets dereferenced and then gets incremented by 1.

So O/P: Hello World
              Iello World



Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig