just had the above question as an interview question, couldn really answer, there is no code under while loop, but just wat does the above code do ?? does it compare or wat?
what does this statement exactly do while (*x++=*y++)..?? this is in C.
Collapse
X
-
Tags: None
-
This should help:
There is almost nothing to explain if you know C Language. Here is the program:#include void copyString(const char *src, char *dest);int main() {char str1[100];char str2[100];printf("Enter the string: ");gets(str1);copyString(str1, str2);printf("Copied string: %s\n", str2);return 0;}void copyString(const char *src, char *dest) {while (*dest++ = *src++);}As you can see actually copying is done in just one line of code. While loop stops after it reaches zero value and all strings in C language are null-terminated strings (ending with 0x00 byte at the end of string, which is zero).Testing:Enter the string: Hello world, we have copy function for string!Copied string: Hello world, we have copy function for string! Note: You should not be using gets() in real application, because it is not possible to limit number of the characters to be read thus allowing to overflow buffer. You might get a warning in line with our while loop if you are compiling with -Wall option in GCC, what does -Wall is that it checks for questionable places. This place for compiler is questionable because we have assignment operation inside while loop expression. Most of the times this is common mistake in programming, but not in this situation. Compiler just give a notice for developer that we should be careful.
Comment