Hi,
did not understand a point of following the program
pch-str + 1
exactly what`s going on here?
1 2 3 4 5 6 7 8 9
int main ()
{
char str[] = "This is a sample string";
char * pch;
pch=strrchr(str,'s');
printf ("Last occurence of 's' found at %d \n",pch-str + 1);
return 0;
}
int main ()
{
char str[] = "This is a sample string";
char * pch;
pch=strrchr(str,'s');
printf ("Begining of the str: %p \n",str);
printf ("Last occurence of 's' found at address: %p \n", pch);
printf ("Last occurence of 's' found at %d \n",pch-str + 1);
return 0;
}
My result of this program is:
1 2 3
Begining of the str: 0xbfc57824
Last occurence of 's' found at address: 0xbfc57835
Last occurence of 's' found at 18
subtract 35 and 24 (in HEX) and get 17 (in dec)
17 + 1 = 18