首先strncpy_s的函數原型如下:
errno_t strncpy_s( char *strDest, size_t numberOfElements, const char *strSource, size_t count );
參數就不做介紹了,容易看懂。下面就幾種情況介紹。主要涉及目標內存大小numberOfElements、拷貝的大小count以及源字符串的大小src_len.
1. numberOfElements > count,src_len>count時,復制count到目標區域;
2.numberOfElement>count,src_len<count時,復制src_len到目標區域;
3. numberOfElement<=count,src_len和count的關系任意時,拋出異常。
其中count不確定時,可以用_TRUNCATE代替,即最大可能的復制到目標區域,這時長度取決於1,2。即numberOfElements和src_len中較大的。
_TRUNCATE實際就是個宏定義,int-1.一個很大的數目。