Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   pointers (http://www.go4expert.com/forums/pointers-t23396/)

Vernika Sharma 23Sep2010 19:17

pointers
 
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

jimblumberg 24Sep2010 19:23

Re: pointers
 
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


All times are GMT +5.5. The time now is 05:49.