I've got this tiny program I wrote for a C pointers exercise. I simply take a char array and print it in reverse using a pointer. It works. However, I don't understand why it works. Since I'm starting the for loop at element 9, shouldn't it print 5 undefined elements (usually random junk characters, in my experience) before it gets to the "sdrow"? I feel like I should be filtering the output more.
int main(void)
{
char sentence[10] = "words";
char *ptr = sentence;
for(int i=9;i>=0;i--)
{
printf("%c", *(ptr+i));
}
puts("");
return 0;
}
Output:
sdrow