|
void * memcpy (void * dst, const void * src,size_t count)
{
void * ret = dst;//可支持链式编程,一个函数完成两个功能。
while (count--)
{
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
src = (char *)src + 1;
}
return(ret);
}
void * memset(void *dst, int val,size_t count)
{
//把val传给*dst时两个变量类型要相同,需要用到强制类型转换
//assert(dst);//这里需要检验dst的有效性
char* ret=(char*)dst;
while (count--)
{
*ret++ = (char)val;
}
return dst;
}
char * strstr(const char *str1, const char *str2)
{
char *cp = (char *)str1;
char *s1, *s2;
if (!*str2)
return((char *)str1);
while (*cp)
{
s1 = cp;
s2 = (char *)str2;
while (*s2 && !(*s1 - *s2))
s1++, s2++;
if (!*s2)
return(cp);
cp++;
}
return(NULL);
}
char * strncpy( char * dest, const char * source, unsigned long count )
{
char *p = dest;
while (count && (*p++ = *source++)) count--;
while(count--)
*p++ = '/0';
return(dest);
}
size_t strlen(const char * str )
{
const char *p = str;
while( *p++ ) ;
return( (int)(p - str - 1) );
} |
|