I want to find the last element of an string without the use of any functions.
My code seems to work, but I'm not quite sure why it does work in the way it does and would be glad about input.
Here is the code.
#include <stdio.h>
char *find_last(char *s);
int main(void)
{
char string[] = {"This is an Example"};
find_last(string);
return 0;
}
char *find_last(char *s)
{
/* char last;*/
while (*s++ != 0) {}
printf("*s ist %c", *(s-2));
return 0;
}
I don't understand why I have to print *(s-2) instead of *(s-1) in order to get the last character. (In order to get a futher character back I only have to decrement one more, i.e. *(s-3)).
Oh, and additionally, it seems that the content of *s seems to be the letter O (from me mistaken as zero, until now), independent of the example-string tested with. Why is that? I thought it should be zero (0) because I increment the pointer until it hits the binary zero.
Thanks for any help!