Use of memcpy strcpy strncpy

Source: Internet
Author: User

Tag: String

Strcpy and memcpy are both standard C library functions, which have the following features:

1. strcpy provides string replication. That is, strcpy is only used for string copying. It not only copies the content of the string, but also copies the end character of the string.

The strcpy function is prototype: char * strcpy (char * DEST, const char * SRC );

Notes:

(1) the data type of the operation is char *, and the returned char *

(2) This function will be copied to the character array together with the string ending character '/0'. Therefore, the length of the character array should be at least the length of the string plus 1

(3) DEST is the first address to store the target string. To store DEST + 3 from 3rd characters

(4) The function copies the content of the string until it encounters a character whose first character value is 0 (the character whose first character value is 0 will also be copied ), therefore, if a string or character array contains a large amount of data with a value of 0, you are not advised to use this function for copy operations. The memcpy function is available.


2. memcpy only provides general memory replication, that is, memcpy has no restrictions on the content to be copied, so it is widely used.

The function prototype of memcpy is: void * memcpy (void * DEST, const char * SRC, size_t count );

Notes:

(1) You need to specify the replication length size_t count. memcpy uses the size_t parameter to determine the number of characters to copy (strcpy ends with the "\ 0)

Strcpy and memcpy have the following differences:

1. The copied content is different. Strcpy can only copy strings, while memcpy can copy any content, such as strings, integers, struct, and classes.

2. The replication method is different. Strcpy does not need to specify the length. It ends with the end character "\ 0" of the copied string, so it is prone to overflow. Memcpy determines the copy Length Based on the 3rd parameters.

3. Different purposes. Generally, strcpy is used to copy strings, while memcpy is used to copy other types of data.

3. strncpy

Char * strncpy (char * DST, const char * SRC, size_t count)

This function is used to copy count characters.

 

Note:

(1) Count must be smaller than DST.

(2) After calling this function, you must add the following sentence: DST [count] = '/0'; otherwise, it is not safe, for example, strlen and other functions require that the parameter must be a string ending with '/0.

When count is smaller than the size of SRC, the ending character '/0' of SRC will not be copied. Therefore, an ending character should be added to DST.


This article is from the "from scratch" blog, please be sure to keep this source http://9433707.blog.51cto.com/9423707/1559086

Use of memcpy strcpy strncpy

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.