How does this C copy function break out of the loop?

void copy (char *source, char *dest) {
    while (*dest++ = *source++);
}

      

The char that is presented *source

is copied to the field *dest

it points to. For the next iteration, each char pointer points to the next field in memory, is that correct?

When does this cycle actually stop? The only condition I can think of is that there is no room in memory, but then the function should fail, right?

I'm completely new to C, so forgive me the simple questions.

+3


source to share


3 answers


char

are integral types. Integral types are interpreted as conditional expressions as follows:

 0 -> false
 Anything else -> true

      



Since "lines" in C are zero-terminated (which means 0

or '\0'

), when it reaches the end of the line, it stops.

+3


source


The "result" of the assignment is the right value. So, it x=1;

returns a value; in this case "1".



Your code copies characters until it encounters a 0 termination at the end of the line source

.

+3


source


Your interpretation of the copy is correct. The loop stops when what dest points to zero, i.e. The "\ 0" character. See http://en.wikipedia.org/wiki/Null-terminated_string

+1


source







All Articles