Differences between memset, memcpy, and strcpy

Source: Internet
Author: User
I. function prototype
Strcpy
Extern char * strcpy (char * DEST, char * SRC );
# Include <string. h>
Function: Copies the SRC string ending with null to the array indicated by DeST.
Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings.
Returns the pointer to DeST.

Memcpy
Extern void * memcpy (void * DEST, void * SRC, unsigned int count );
# Include <string. h>
Function: copy count strings from the memory area indicated by Src to the memory area indicated by DeST.
Note: the memory areas specified by Src and DEST cannot overlap. The function returns a pointer to DeST.

Memset
Extern void * memset (void * buffer, int C, int count );
# Include <string. h>
Function: sets the first count byte of the memory area referred to by buffer to character C.
Returns the pointer to the buffer.
 

Ii. Differences
Memset is used to set all memory spaces to a specific character. It is generally used to initialize the defined string to ''or '\ 0 ';
For example, char a [100];
Memset (A, '\ 0', sizeof ());

Memcpy is used for memory copying. It can be used to copy objects of any data type and can specify the length of the copied data;
Example:
Char A [100], B [50];
Memcpy (B, A, sizeof (B); // Note: If sizeof (a) is used, memory overflow may occur.
Mem is a piece of memory. Its length must be remembered by yourself. What is memcpy copied.

Strcpy can only copy strings. It ends copying when '\ 0' is encountered;
For example, char a [100], B [50];
Strcpy (A, B );
If strcpy (B, A) is used, pay attention to whether the length of the string in A (before the first '\ 0') exceeds 50. If it exceeds, it will cause B's
Memory overflow. It will not copy '\ 0', so there is usually a statement:
* A = '\ 0 ';

Iii. Tips
Memset can easily clear variables or arrays of a data structure.
For example:
Struct sample_struct
{
Char csname [16];
Int iseq;
Int itype;
};
For Variables
Struct sample_struct sttest;
Generally, the sttest initialization method is as follows:
Sttest. csname [0] = '\ 0 ';
Sttest. iseq = 0;
Sttest. itype = 0;
Memset:
Memset (& sttest, 0, sizeof (struct sample_struct ));
If it is an array:
Struct sample_struct test [2, 100];
Memset (test, 0, sizeof (struct sample_struct) * 100 );

 

Strcpy is a copy string, ending with \ 0 (that is, once the memory address copy process with a data value of 0 is stopped)
The prototype of strcpy is
Char * strcpy (char * DEST, const char * SRC)
Memcpy copies memory data of a specified size N after a given source and target, regardless of the copied content (not limited to characters)
The prototype of memcpy is
Void * memcpy (void * DEST, const void * SRC, size_t N );

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.