pointers

Discussion in 'C' started by Vernika Sharma, Sep 23, 2010.

  1. Vernika Sharma

    Vernika Sharma New Member

    Joined:
    Sep 23, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    char* myFunc (char *ptr)
    {
     ptr += 3;
     return (ptr);
    } 
    int main()
    {
     char *x, *y;
     x = "HELLO";
     y = myFunc (x);
     printf ("y = %s \n", y);
     return 0;
    }
    
    its output is LO
    How? I am not able t understand
     
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    If you look at "HELLO" as an array of characters:
    x[0] = 'H', x[1] = 'E', x[2] = 'L', x[3] = 'L', x[4] = 'O', x[5] = '\0'

    Your 'ptr' variable points to x[0] (the start of your array). Now add 3 to 'ptr'. Where do you end up??

    Jim
     

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