浅分析C++中memset、memcpy、strcpyの違い



#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
",str1);

 
       char a[3];
       memset(a, 'a', sizeof(a)-1);
       memset(&a[2], '\0',1);
       printf("%s
",a);

     
       memcpy(str1, str2, strlen(str2));
       printf("%s
",str1);

}