注意:
1、需考虑异常情况。可用宏assert来完成
2、strcpy会拷贝末尾的 '\0'
#include <stdio.h>
#include <assert.h>
char* strcpy(char* des, const char* src)
{
assert((des!=NULL) && (src!=NULL));
char* ret= des;
while(*des++ = *src++);
return ret;
}
本文介绍了一种使用C语言实现安全字符串复制的方法,通过使用宏assert检查指针有效性,避免了strcpy函数可能引发的缓冲区溢出问题。文章提供了一个改进的strcpy函数实现,确保在目标和源字符串都不为空的情况下进行复制。
注意:
1、需考虑异常情况。可用宏assert来完成
2、strcpy会拷贝末尾的 '\0'
#include <stdio.h>
#include <assert.h>
char* strcpy(char* des, const char* src)
{
assert((des!=NULL) && (src!=NULL));
char* ret= des;
while(*des++ = *src++);
return ret;
}
1964

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