Thread: C Code doubt

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    C Code doubt


    what does the following code explains?

    char c[]="GATE2011";
    char *p=c;
    printf("%s",p+p[3]-p[1]);
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,407
    Rep Power
    1871
    Well did you run it to find out?

    > printf("%s",p+p[3]-p[1]);
    First you write it as
    Code:
    printf("%s",&p[ p[3]-p[1] ] );
    Then you make the appropriate substitutions for p[3] and p[1] to get the actual values.

    Then you look up 'X' and 'Y' (your actual two letters) in an ASCII table, to work out the maths.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo