PREV INDEX NEXT

Author: Stan Eisenstat
Subject: Re: [Cs223] strncmp
Date: Friday, 20 Mar 2020, 19:30:54


    > Message Posted By: Unknown
    >
    > For strncmp(s1, s2, n), if str1 and str2 are both null terminated and
    > strlen(s1) < n < strlen(s2), does the function compare at most strlen(s1)
    > or n bytes of s1 and s2? Do we need to worry about strncmp accessing
    > memory after null terminator of s1?

strncmp() compares character by character, left to
right, until the two strings differ.  Since the null
terminator of s1 does not match the corresponding
character in s2, it will stop at that character.

--Stan-
PREV INDEX NEXT