一、strcpy()实现, 字符串拷贝.
char *strcpy(char *strDest, const char *strSrc)
{
assert((strDest!=NULL) && (strSrc !=NULL));
char *address = strDest;
while( (*strDest++ = * strSrc++) != '\0')
NULL ;
return address ;
}
二、memcpy, 拷贝不重叠的内存块
void *memcpy(void* pvTo, void* pvFrom, size_t size)
{
assert(pvTo != NULL && pvFrom != NULL);
void* pbTo = (char*)pvTo; //必须强制转换
void* pbFrom = (char*)pvFrom;
/* 内存块重叠吗?如果重叠,就使用memmove */
assert(pbTo>=pbFrom+size || pbFrom>=pbTo+size);
while(size-->0)
*pbTo++ == *pbFrom++;
return pvTo;
}
char d[]="123";
strcpy(d,s);
char *strcpy(char *strDest, const char *strSrc)
{
assert((strDest!=NULL) && (strSrc !=NULL));
char *address = strDest;
while( (*strDest++ = * strSrc++) != '\0')
NULL ;
return address ;
}
二、memcpy, 拷贝不重叠的内存块
void *memcpy(void* pvTo, void* pvFrom, size_t size)
{
assert(pvTo != NULL && pvFrom != NULL);
void* pbTo = (char*)pvTo; //必须强制转换
void* pbFrom = (char*)pvFrom;
/* 内存块重叠吗?如果重叠,就使用memmove */
assert(pbTo>=pbFrom+size || pbFrom>=pbTo+size);
while(size-->0)
*pbTo++ == *pbFrom++;
return pvTo;
}
很好的实例:
char s[]="123456789";char d[]="123";
strcpy(d,s);