Thursday, 16 October 2014

C Question

Q: int main()
{
  char arr[]  = "geeksforgeeks";
  char *ptr  = arr;
  while(*ptr != '\0')
      ++*ptr++;
  printf("%s %s", arr, ptr);
  getchar();
  return 0;
}
Output:  hffltgpshfflt
Explanation:
The crust of this question lies in expression ++*ptr++.
If one knows the precedence and associativity of the operators then there is nothing much left. Below is the precedence of operators.
   Postfixx ++            left-to-right
   Prefix  ++             right-to-left
   Dereference *          right-to-left
Therefore the expression ++*ptr++ has following effect
Value of *ptr is incremented
Value of ptr is incremented

No comments:

Post a Comment