Is strncpy() a specialization of memcpy()?

后端 未结 5 1616
伪装坚强ぢ
伪装坚强ぢ 2021-01-25 01:59

Just curious to know (as we use these functions often). I don\'t see any practical difference between strncpy() and memcpy(). Isn\'t it worth to say that effectively,

         


        
5条回答
  •  野性不改
    2021-01-25 02:16

    Adding on to what the others have said, the type of the src and dst pointers does not matter. That is, I can copy a 4 byte integer to 4 consecutive characters of 1 byte like this:

    int num = 5;
    char arr[4];
    memcpy(arr, &num, 4);
    

    Another difference is that memcpy does not look ofr any characters (such as NULL, by strncpy). It blindly copies num bytes from source to destination.

    Edited: Properly formatted the code

提交回复
热议问题