pointers

Vernika Sharma's Avatar, Join Date: Sep 2010
Newbie Member
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's Avatar
Ambitious contributor
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