The function strcmp
expects pointers as the arguments. See the prototype: int strcmp(const char * s1, const char * s2)
;
string1[2]
and string2[5]
are just characters 'l'
and 'o'
.
printf("%d", strcmp(string1[2], string2[5])); // will not compile
printf("%d", strcmp(&string1[2], &string2[5])); // will return -3 with my compiler GCC 5.3.0
&string1[2]
is a pointer to string1[2]
character
&string2[5]
is a pointer to string2[5]
character
Note:
The strcmp() and strncmp() functions return an integer less than,
equal to, or greater than zero if s1 (or the first n bytes thereof) is
found, respectively, to be less than, to match, or be greater than s2.
However, the exact return value depends on the implementation.
For this implementation:
int strcmp_fast (const char * s1, const char * s2)
{
for(; *s1 == *s2; ++s1, ++s2)
if(*s1 == 0)
return 0;
return *(const unsigned char *)s1 - *(const unsigned char *)s2;
}
the:
printf("%d", strcmp_fast(&string1[2], &string2[5]));
printed value is -3
.