Thread: Cannot understand the output.

  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59

    Cannot understand the output.

    I was browsing through some output questions in C language, where I found the following one.

    Code:
    #include<stdio.h>
    
    int main()
    {
      char arr[]  = "geeksforgeeks";
      char *ptr  = arr;
     
      while(*ptr != '\0')
          ++*ptr++;
      printf("%s %s", arr, ptr);
     
      getchar();
      return 0;
    }
    I ran the above code, and the output came out to be 'hffltgpshfflt'. Now the expression ++*ptr++ is quite confusing and it's difficult to understand what's exactly happening there. Any help regarding that would be appreciable. (Note- I do remember precedence rules/associativity but how that expression will get evaluated is my problem).

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    I'm not 100% sure on this so I'll just misinform you and then someone can come correct me.

    If I recall correctly the post increment operator actually has very high precedence, it is just evaluated last.

    So ++*ptr is ++(*ptr) because of the order of operands.
    *ptr++ is *ptr; ptr++ because the ++ is higher precedence but evaluated last.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    i would think the expression equates to ++*(ptr) but my thoughts have been known to deceive me

    EDIT:
    they did deceive me.my apologies
    Last edited by africanwizz; 02-06-2014 at 11:19 AM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I always keep a C operator precedence chart handy: C Operator Precedence Table. I mostly have it memorized by now, but it's nice to reassure myself for complicated stuff.

    Note that postfix ++ has the highest precedence, followed by * (dereference) and prefix ++. * and prefix ++ have equal precedence, but right-to-left associativity, meaning the right-most of those gets evaluated first.

    DeadPlanet is right:

    *ptr is not '\0' (it's 'g'), so:
    1. ptr++ is evaluated first, but the value of that expression is just ptr, and the increment happens last (at the next sequence point, which is the end of the statement here, i.e. the ; on line 9)
    2. We apply * to ptr (the value before the increment happens), to dereference and get the char there (e.g. 'g' the first iteration)
    3. We then apply the prefix ++ to *ptr, turning 'g' into 'h' (assuming a charset where letters are adjacent and in alphabetical order).
    4. The increment of ptr from the postfix ++ happens, moving ptr to the next spot in the string.
    5. Go back to the top of the loop, and check *ptr again.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59
    Thanks for the detailed reply anduril462, surely helped a lot to get things clear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I don't understand this gdb output...
    By überfuzz in forum C Programming
    Replies: 2
    Last Post: 05-09-2013, 03:30 AM
  2. Cannot able to understand the output
    By raj.knd in forum C Programming
    Replies: 7
    Last Post: 12-07-2011, 06:29 AM
  3. i cant understand why i get such an output??
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 10-16-2008, 06:06 AM
  4. tricky output problem, dont understand
    By panfilero in forum C Programming
    Replies: 6
    Last Post: 11-12-2005, 12:30 AM
  5. cannot understand this output - atoi
    By aldajlo in forum C Programming
    Replies: 11
    Last Post: 10-02-2004, 11:48 AM