How does this C copy function exit its loop?

后端 未结 3 1551
心在旅途
心在旅途 2021-01-21 03:14
void copy (char *source, char *dest) {
    while (*dest++ = *source++);
}

The char that is represented by *source is copied to the field <

相关标签:
3条回答
  • 2021-01-21 03:28

    chars are integral types. Integral types are interpreted as conditionals in the following way:

     0 -> false
     Anything else -> true
    

    Since "strings" in C are null-terminated (meaning 0 or '\0') when it reaches the end of the string it stops.

    0 讨论(0)
  • 2021-01-21 03:40

    The 'result' of an assignment is the right-hand value. So x=1; actually returns a value; in this case, '1'.

    Your code copies characters until it encountered the terminating 0 at the end of the source string.

    0 讨论(0)
  • 2021-01-21 03:50

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

    0 讨论(0)
提交回复
热议问题