strlen returns the number of characters preceding the terminating null character. The strlen implementation might look like this:
size_t strlen(const char * str) { const char *s; for (s = str; *s; ++s) {} return(s - str); }
These are the specific dereferences of the implementation of s , where s may contain undefined values. This is equivalent to this:
int a; int* p = &a; *p;
So, for example, if this were done (which leads to the fact that strlen gives the wrong output):
char buffer[10]; buffer[9] = '\0'; strlen(buffer);
Is this behavior undefined?
source share