Implementation of common library functions in C language

Source: Internet
Author: User

1.memcpy function

The memcpy function is used to copy the resource memory (the area of memory pointed to by SRC) to the target memory (the area of memory pointed to by dest); How many copies? There is a size variable that controls the number of bytes copied;

Function prototypes: void *memcpy (void *dest, void *src, unsigned int count);

Usage: You can copy any type of object, because the parameter type of the function is void* (undefined type pointer), that is, the arguments passed in can be int*,short*,char* and so on, but because the process of the function copy is a byte-byte copy, So the actual operation of the time to void* forced conversion to char*, so that when the pointer is added to ensure that each time a byte;

#include <stdio.h>#include<iostream>#include<assert.h>using namespacestd;/*The *******memcpy () function prototype is: void* memcpy (void* dest, void* src, size_t n); Returns an empty type pointer to Dest *********///the reason for returning the void* type is to use chained expressions, i.e. strlen ((char*) (memcpy (Dest,src,n)), which directly calculates the length of the dest, which is a cleaner program code/** * * Note the use of the void* pointer, which allows the function to pass in any type of pointer data * * **/void* MEMCPY (void*dest,void*src, size_t N) {Assert (dest! = NULL) && (src! =NULL)); Char*dest_t = (Char*) dest;//Converted to a copy of a character type, because the process of the copy of the function is a byte-byte copy,//so the actual operation of the time to void* forced into the char*,    Char*src_f = (Char*) src;//this will only be guaranteed to add one byte at a time when the pointer is added.     while(n-->0)    {        * (dest_t++) = * (src_f++); }    returnDest//void* Be sure to return a value (pointer), this is not the same as void! function returns a pointer to Dest}intMain () {inta[5] = {0,1,2 }; int*b =New int[3]; void*c = memcpy (b, A,3*sizeof(int));//sizeof () can use a type parameter, or it can pass in the actual variable     for(inti =0; I <3; i++)//arrays do not degenerate into pointers when they are parameters, which counts the total memory occupied by the array .{cout<< B[i] <<Endl; }    int*temp = (int*) C; Temp++; cout<<Endl; cout<< *temp <<Endl; return 0;}

Note 1: Compared with strcpy, memcpy does not meet the end of ' n ', but is bound to copy the nth bytes.

2: If the target array dest itself already has data, after executing memcpy (), the original data will be overwritten (up to N).

// memcpy is used to make a memory copy, you can copy any data type object, you can specify the length of the copied data;  Char a[[], b[]; memcpy (b, A,sizeof// Note that using sizeof (a) will cause the memory address of B to overflow.  strcpy can only copy a string, it encounters '"" to end the copy; example:  char a[ [], b[];      strcpy (A, b);  
2.strcpy function
#include <stdio.h>#include<iostream>using namespacestd;/*The *******strcpy () function prototype is: Char *strcpy (char* dest, const char *SRC); Returns a pointer to Dest *********///the reason for returning the char* type is to use the chain expression, strlen (strcpy (DEST,SRC)), so that the length of the dest can be calculated directly, and the program code is more conciseChar* STRCPY (Char*dest,Char*src) {    if(dest = = NULL | | src = =NULL)returnNULL; Char*res = dest;//Save original DST's first address     while(*src! =' /')    {        *dest = *src; Dest++; SRC++; }    *dest =' /'; returnRes;}intMain () {Char*SRC ="Hello World"; Char*dest =New Char; //strcpy (DEST,SRC);    intLen =strlen (strcpy (DEST,SRC)); cout<< Len <<Endl; cout<< dest <<Endl; return 0;}

Implementation of common library functions in C language

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.