What will be printed out as a result of the following code execution?

#include <iostream>

int main() {
    char src[] = "Hello, world";
    char dst[ sizeof src ];
    const char * psrc = src;
    char * pdst = dst;
    while( *pdst++ = *psrc++ );
    std::cout << dst << std::endl;
    return 0;
The while( *pdst++ = *psrc++ ) line is of a most interest here. What happens here? Pointers' postfix increment that return their copies is performed first. Dereference and assignment of a source symbol value to a receiver symbol happens after. And, finally, (loop ending condition!) the result - symbol value - is being checked for 0 (null-terminator at the end of a string). To sum up: given code is a tricky version of a string copy approach.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback