我是靠谱客的博主 开放发箍,最近开发中收集的这篇文章主要介绍【学习总结】模拟实现strcpy和strlen函数以及几个需要注意的点模拟实现strcpy和strlen函数的几个注意点,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
如有不足还请指正。
模拟实现strcpy和strlen函数的几个注意点
资料:《高质量C++/C编程》
在模拟实现函数的过程中,不只要只实现个功能,还要注重如何让代码更加健壮!
先来给出code,下面再详细解释:
#include <stdio.h>
#include <string.h>
#include <assert.h>
char* my_strcpy(char *str_dest, const char *str_src)
{
assert(str_dest != NULL);
assert(str_src != NULL);
char *tmp = str_dest;
while((*tmp++ = *str_src++))
{
;
}
return str_dest;
}
unsigned int my_strlen(const char *str)
{
int count = 0;
assert(str != NULL);
while(*str != '