#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 发布