Help with char array n char pointer

Discussion in 'C' started by IndiraP, Nov 10, 2012.

  1. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    #include<stdio.h>
    void main()
    {
    char a[]="Gate2011";
    char *p=a;
    printf("%s", p + p[3] - p[1]);
    }

    Outputs 2011
    Can somebody me the explanation of how it is???
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    What are p[3] and p[1]?
     
  3. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0

    I think pointer accessing array elements.. i dont know when i run that code i got output 2011...
    if u can understand how it works..do explain me..
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Sorry, let me clarify. In order to work out what is going on, you need to evaluate the expression p+p[3]-p[1]. And as the output is "2011" that means you need to work out why the expression evaluates to &p[4].

    My question did not indicate I do not know the answer. I know exactly why it does what it does. The point is to help YOU figure it out.

    So have a look at p, work out what it is and what it points at, then if you need to, read the book on character strings again, then you should be able to work out what p[0], p[1], p[2] and so on are.
     
  5. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    I am extremely sorry sir..i never meant that u donot know it..
    Thank u for guiding me..
    n p indicates the starting position here means it points to 'G'
    p[3] means it points to 4th position from 'G' means its 'e'
    p[1] means it points to 1st position means its 'a'..
    Individually i can get through here...but the arithmetics r what i am not reachable..
    help me sir..
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    OK, so do you know that 'a' is in fact an integer constant?
    Find its value, and find the value of 'e', you will be able to work out what p+'e'-'a' is.
     
    IndiraP likes this.
  7. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    Okay...!!!
    So... 'e'-'a'=101-97=4
    And since p is a pointer, p+4 indicates the control to jump to p[4]...and thats where 2 resides..and since %s is mentioned, everything from 2 will be printed...rite..??
    and so the 2011....
    Yeah...I got it..:D
    Thank U Sir... :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice