Saturday, May 22, 2010

Wat is diff b/w *p++ and ++*p?

it is a c language question ask in interview

Wat is diff b/w *p++ and ++*p?
It is an increment that happens before or after the variable is returned. The * indicates p must be a pointer.





If p is 3, then





p++ will return 3 and then increment p by 1, which means p is 4





++p will increment p by 1, which means p is 4, then return p.
Reply:*p++ returns the contents at p and then increments p by the size of the type that p points to.





++*p returns the contents of p incremented by 1.





If char *p = "159"


ch = *p++ would return '1' and then p would = "59"


ch would = '1'





If char *p = "159"


ch = ++*p would increment the value at p


ch would = '2' and p = "259"
Reply:*p++ it is add a one to 'p' and then it is return and it will return and then add a one as 'p' is a pointer
Reply:Easy.


One p is in the prefix form and another one is in postfix form.


Let take an example to explain


x=1;


y=++x;


Ans y=2 and then x=2


We first increment x, like y=x+1 where x=1





x=1;


y=x++


Ans y=1 and then x=2


We first check for y since the + sign is found behind x. We put y=1 and then increment x.





Tips: Look for the position of the sign and then calculate. It will help u.

chelsea flower show

No comments:

Post a Comment