I have seen the standard implementation of strlen using pointer as:
int strlen(char * s) {
char *p = s;
while (*p!='\0')
p++;
return p-s;
}
I get this works, but when I tried to do this using 3 more ways (learning pointer arithmetic right now), I would want to know whats wrong with them?
This is somewhat similar to what the book does. Is this wrong?
int strlen(char * s) { char *p = s; while (*p) p++; return p-s; }
I though it would be wrong if I pass an empty string but still gives me 0, kinda confusing since p is pre increment: (and now its returning me 5)
int strlen(char * s) { char *p = s; while (*++p) ; return p-s; }
Figured this out, does the post increment and returns +1 on it.
int strlen(char * s) { char *p = s; while (*p++) ; return p-s; }