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 returns their copies is performed first. Dereference and assignment of a source character value to a receiver character happens after. And, finally, (loop ending condition!) the result - character 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
Sign Up Now
or Subscribe for future quizzes