strcpy, memcpy, memset function

Source: Internet
Author: User

I. strcpy function prototype declaration: Char *strcpy (char* dest, const char *SRC); Header files: #include <string.h> and #include <stdio.h> functions: Copy a String that starts from the SRC address and contains a null terminator to an address space starting with dest Description: The memory areas referred to by SRC and dest cannot overlap and dest must have sufficient space to accommodate the SRC string. Returns a pointer to the dest. Implementation code:
Char* STRCPY (Char* Strdest,Const Char*strsrc) {    if((null==strdest) | | (null==strsrc)) Throw "Invalid argument (s)"; Char* Strdestcopy =strdest;  while((*strdestcopy++=*strsrc++)! =' /'); returnstrdest;} 

Two. memcpy function

Function Prototypes:

void *memcpy (void *dest, const void *SRC, size_t n);

function function:

Copies n bytes from the starting position of the memory address referred to by the source SRC to the starting position of the memory address referred to by the target dest.

Header file:

C language include<memory.h>c++include<memory> Description: The memory areas referred to by 1.source and Destin may overlap, but if the memory areas referred to by source and Destin overlap, Then this function does not ensure that the overlapping area of the source is not overwritten before the copy. The use of memmove can be used to process overlapping areas. The function returns a pointer to Destin. 2. If the target array Destin itself already has data, after executing memcpy (), the original data will be overwritten (up to N). If you want to append data, add the destination array address to the address you want to append the data to after each execution of memcpy. Note: Both source and Destin are not necessarily arrays, and any readable and writable space is available. strcpy and memcpy mainly have the following 3 aspects of the difference. 1, the content of the copy is different. strcpy can only copy strings, while memcpy copies arbitrary content, such as character arrays, integers, structs, classes, and so on. 2, the method of replication is different. strcpy does not need to specify a length, it encounters the string terminator of the copied character "\" before it ends, so it is prone to overflow. memcpy determines the length of the copy according to its 3rd parameter. 3, the use of different. It is common to use strcpy when copying strings and to replicate other types of data memcpy

Three. Memset function

void *memset (void *s, int ch, size_t n); function interpretation: replaces the first n bytes in s (typedef unsigned int size_t) with CH and returns s (assigned in bytes). Memset: The function is to populate a block of memory with a given value, which is the fastest way to clear 0 operations on a larger struct or array
Char buffer[];memset (buffer,0,sizeof(char) *) ;

strcpy, memcpy, memset function

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.