What is the difference between memcpy() and strncpy() given the latter can easily be a substitute for the former?

前端 未结 3 1371
慢半拍i
慢半拍i 2021-02-13 21:52

What is the significant difference between memcpy() and strncpy()? I ask this because we can easily alter strncpy() to copy any type of da

3条回答
  •  礼貌的吻别
    2021-02-13 22:38

    The simple difference is that memcpy() can copy data with embedded null characters (aka the string terminator in C-style strings) whereas strncpy() will only copy the string to the maximum of either the number of characters given or the position of the first null character (and pad the rest of the strings with 0s).

    In other words, they do have two very different use cases.

提交回复
热议问题