Thread: Why both statements give same result ?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    59

    Exclamation Why both statements give same result ?

    Code:
    int main()
    {
        char *arr[] = { "Hello", "World", "Good", "Morning", NULL };
        char **ptr = arr;
    
        printf("\n %u", ptr);
        printf("\n %c", (*++ptr)[0]);
        printf("\n %u", ptr);
        printf("\n %c", *++ptr[0]); //1.
        printf("\n %u", ptr);
        printf("\n %c", *++ptr[1]); //2. This & 1 give same result.
        printf("\n %u", ptr);
    
        printf("\n\n");
        return 0;
    }
    Output : 
    3220482380
    W
    3220482384
    o
    3220482384
    o
    3220482384
    I get the same result in both 1 & 2. That means ptr does not get incremented ?

    Regards

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Yes, because indexing( the [] operator ) has higher priority than pre-incrementing( the ++ one ).

    By the way, you shouldn't define your strings like that. Use "const char* " instead.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I test for a result, but any result over 1 fails
    By jeremy duncan in forum C++ Programming
    Replies: 10
    Last Post: 05-23-2012, 01:23 PM
  2. No result out for this CP
    By leevenchoon in forum C Programming
    Replies: 12
    Last Post: 07-10-2011, 11:36 AM
  3. What would be the result...
    By sreeramu in forum C Programming
    Replies: 8
    Last Post: 03-25-2008, 04:43 AM
  4. No Result when run *.exe
    By hednast in forum C Programming
    Replies: 6
    Last Post: 08-23-2005, 12:50 AM
  5. Replies: 1
    Last Post: 06-29-2004, 05:23 PM