Originally Posted by gonX
What don't you understand about it?
Here's the code indented for readability:
int compare_string(char *str1, char *str2)
int count = 1;
if ( *str1 == '\0' || *str2 == '\0' )
if( *str1 == '\0' && *str2 == '\0' )
Anyway, the function, given two pointers to a C-string, checks each character of each until it reaches the end of one of the strings, at which point the loop stops. As all C-strings should be terminated with a zero (null termination), once we reach the termination we know we are at the end of the string.
While the end of each string has not yet been reached and the current character of the first and second strings match, the loop increments each string pointer by one (so that the next loop iteration checks the next character in each string) and also increments a count variable which is used in the return value of the function to tell the position of the first different character, if the strings differ at all.
If by the time the loop has finished and both
strings have reached the end, then both strings are considered equal and the function returns 0 (comparison functions return 0 if the two items match). Otherwise, the function returns the position of the first different character, thus greater than zero, signalling that the two strings are not the same.