C語言-使用strncpy複製字串

當使用strncpy來複製字串時, 需注意strncpy並不處理字串結尾\0
所以需要先將該buf清空或在最後加\0

Example:

memset( buf,0,sizeof(buf));
strncpy( buf, string, sizeof(buf)-1);
or
strncpy( buf, string, sizeof(buf)-1);
buf[ sizeof(buf)-1 ] = ‘\0’;

One thought on “C語言-使用strncpy複製字串

發表迴響

Copy Protected by Chetan's WP-Copyprotect.