strcmp(1): code clarification

This commit is contained in:
dtb 2024-07-15 14:40:26 -06:00
parent efb3ce626d
commit becb3bac4e
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B

View File

@ -30,10 +30,11 @@ int main(int argc, char *argv[]){
return EX_USAGE;
}
for (; *argv[1] != '\0'; ++argv[1]) { /* iterate chars in ref */
/* iterate argc */
for (size_t i = 2 /* ref cmp */; i < argc; ++argv[i], ++i) {
/* this doesn't overrun because of nul termination */
/* This compares the Nth character of arg[2] onward with argv[1]'s Nth
* character, rather than comparing each arg with argv[1] sequentially. */
for (; *argv[1] != '\0'; ++argv[1]) { /* iterate chars in argv[1] */
for (size_t i = 2; i < argc; ++argv[i], ++i) { /* iterate &argv[2] */
/* this never overruns because of nul termination */
if (*argv[i-1] != *argv[i]) { return *argv[i-1] - *argv[i]; }
}
}