A brief analysis of memset,memcpy,strcpy difference _c language in C + +

Source: Internet
Author: User
Copy Code code as follows:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

memcpy: Copying by byte
Prototype: extern void* memcpy (void *dest,void *src,unsigned int count)
Function: Copy count bytes from the memory area referred to by SRC to the memory area referred to by dest;
With 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: Copy string, and end with ' I '
Prototype: extern char *strcpy (char *dest,char *src)
function: To copy the string src refers to the end of ' dest ' to the array of the reference;
Description: The areas of memory that Src and dest refer to are not overlapped and dest must have enough space to hold the string. Returns the Dest pointer.
Char *strcpy_su (char *dest,char *src)
{
ASSERT ((dest!=null) && (src!=null));
char *address = dest;
while ((*dest++=*src++)!= ' ")
Continue
return dest;
}
Memset: Sets the first count byte of the memory area referred to by buffer, substituting character C for
Prototype: 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], ' n ', 1);
printf ("%s\n", a);


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

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.