代碼如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
//memcpy:按字節復制
原型:extern void* memcpy(void *dest,void *src,unsigned int count)
//功能:由src所指內存區域復制count個字節到dest所指的內存區域;
//同strcpy
void *memcpy_su(void *dest, void *src, unsigned int count)
{
assert ((dest!=NULL)&&(src!=NULL));
char* bdest = (char*)dest;
char* bsrc = (char*) src;
while(count-->0)
*bdest++ = *bsrc++;
return dest;
}
//strcpy:復制字符串,遇到'\0'就結束
//原型:extern char *strcpy(char *dest,char *src)
//功能:把src所指由'\0'結束的字符串復制到dest所指的數組中;
//說明:src和dest所指內存區域是不可以重疊的且dest必須有足夠的空間來容納字符串。返回dest指針。
char *strcpy_su(char *dest,char *src)
{
assert((dest!=NULL)&&(src!=NULL));
char *address = dest;
while((*dest++=*src++)!='\0')
continue;
return dest;
}
//memset:設置buffer所指的內存區域的前count個字節,用字符c來代替
//原型:extern void *memset(void *buffer,int c,int count);
void *memset_su(void *buffer, int c, int count)
{
assert ((buffer!=NULL));
char* buffer2 = (char*)buffer;
while(count-->0)
*buffer2++ = c;
return buffer;
}
void main()
{
char str1[100]="abchjhgjghjgjgh";
char str2[50]="efghdfkdjf";
strcpy(str1, str2);
printf("%s\n",str1);
char a[3];
memset(a, 'a', sizeof(a)-1);
memset(&a[2], '\0',1);
printf("%s\n",a);
memcpy(str1, str2, strlen(str2));
printf("%s\n",str1);
}