【c语言】自己的strncpy与库里的strncpy区别

来源:互联网 发布:网络三国官网 编辑:程序博客网 时间:2024/04/28 06:56
// 自己的strncpy与库里的strncpy区别#include <stdio.h>#define CHAR char#define ULONG unsigned longCHAR *VOS_strncpy(CHAR *pcDest, const CHAR *szSrc, ULONG ulLength){CHAR *pcPoint = pcDest;// 对指针进行判空if ((NULL == szSrc)||(NULL == pcDest)){return NULL;}while (ulLength && (*pcPoint = *szSrc)){pcPoint++;szSrc++;ulLength--;}// 区别来了,如果拷贝完了原先的字符串里还有东西就在后边放一个‘\0’if (!ulLength){*pcPoint = '\0';}return pcDest;}int main(){CHAR szStrBuf[] = "1234567890";CHAR szStrBuf1[] = "1234567890";strncpy(szStrBuf, "ABC", strlen("ABC"));VOS_strncpy(szStrBuf1, "ABC", strlen("ABC"));printf("Str1 = %s\nStr2 = %s\n", szStrBuf, szStrBuf1);return 0;}




0 0