I am not a C developer or don't know much about C, but I came across this C interview question:
int toto(char *a, char *b){
while(*a++ == *b++);
return(*a == 0 && *b == 0);
}
I spent a lot of time trying to figure it out, and after reading a few things on the Internet, I seemed to understand what he was trying to do, but still there were some strange behaviors. From what I understand (please correct me if I'm wrong), this piece of code will go through two lines (char arrays) and determine if they are equal to the last character and return true only if the last character is different. return (*a == 0 && *b == 0)checks for an integer 0that all lines end with C. This only happens after the loop exits, when the two characters are not equal before the increment occurs; therefore, if the last two characters are not equal, it will increment them to 0int and go into the return statement. I also noticed that if the strings differ by 1, then it still returns true, if the strings are equal ton-1, eg:
char a[] = "ggr"
char b[] = "ggre"
char a[] = "ggr"
char b[] = "ggf"
, , , :
char a[] = "abcd";
char b[] = "abcd";
char a[] = "abc"
char b[] = "abc"
, abc false, , abcd. , -, , , .
- , -, . , , .
: , , , , , .