strncpy_s和_TRUNCATE


首先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.一個很大的數目。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM