#include <iostream>
#include <assert.h>
using namespace std;
//实现strncpy函数,注意啊,中间带个N,就是从源字符串复制多少个过来,
//返回值为目标字符串的地址是为了实现链式操作
char* strncpy(char *strDest, const char *strSrc, unsigned int count)//注意这里的CONST,指向的内容不能修改
{
assert(strDest != NULL && strSrc != NULL); //断言保证传进来的两个参数不是空
char * address = strDest;
while ( count-- && *strSrc != '\0') //一直赋值,赋值为‘\0’的时候结束
{
*strDest++ = *strSrc++;
}
*strDest = '\0';
return address;
}
int main(void)
{
char *s = "hello world";
char *t = "hello";
char a[10] = {0};
strncpy(a, t, 4);
cout<<a;
system("pause");
return 0;
}实现strncpy函数
最新推荐文章于 2023-06-07 20:50:53 发布
本文展示了一个用C++实现的strncpy函数,该函数能够从源字符串复制指定数量的字符到目标字符串,并确保目标字符串以null终止。通过具体实例演示了如何使用此自定义函数。
9250

被折叠的 条评论
为什么被折叠?



