From becb3bac4eb308584ab13f558fe17e692cc8f8bb Mon Sep 17 00:00:00 2001 From: DTB Date: Mon, 15 Jul 2024 14:40:26 -0600 Subject: [PATCH] strcmp(1): code clarification --- src/strcmp.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/strcmp.c b/src/strcmp.c index 784029c..33eab10 100644 --- a/src/strcmp.c +++ b/src/strcmp.c @@ -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]; } } }