C language memccpy () function: copying content in memory
header file:
To define a function:
void * memccpy (void *dest, const void * src, int c, size_t N);
Function Description: memccpy () is used to copy src refers to the contents of the memory of the first n bytes to the address referred to Dest. Unlike memcpy (), memccpy () checks for the presence of parameter C at the time of replication, if it returns the next byte address in dest with a value of C.
Return value: Returns the next byte pointer to dest with a value of C. A return value of 0 indicates that there are no bytes of C in the first n bytes of the memory referred to in SRC.
Example
#include <string.h>
Main () {
char a[] = "string[a]";
Char b[] = "string (b)";
Memccpy (A, B, ' B ', sizeof (b));
printf ("memccpy ():%s\n", a);
}
Execution results:
C language memcpy () function: Copy memory content (ignore)
header file:
memcpy () is used to copy memory, and its prototype is:
void * memcpy (void * dest, const void * src, size_t num);
memcpy () copies the first num bytes of the memory content of SRC to the memory address dest refers to.
memcpy () does not care about the data types being replicated, but replicates in bytes, which gives a lot of flexibility to the use of functions and can be replicated for any data type.
It is to be noted that:
The dest pointer is allocated enough space, or greater than the NUM byte. If there is no space allocated, a break error occurs.
The memory space referred to by dest and SRC cannot overlap (if overlapping occurs, using Memmove () is more secure).
Unlike strcpy (), memcpy () copies the num byte completely, and does not end with "".
Return value returns a pointer to Dest. Note that the returned pointer type is void and is typically used for coercion type conversions.
code example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N (TEN)
int main ()
{
char* p1 = "ABCDE";
char* P2 = (char*) malloc (sizeof (char) * N);
char* p3 = (char*) memcpy (P2, p1, N);
printf ("P2 =%S\NP3 =%s\n", p2, p3);
Free (p2);
P2 = NULL;
P3 = NULL;
System ("pause");
return 0;
}
Run Result:
Code Description:
1 The Code first defines P1,P2,P3 three pointers, but slightly differently, P1 points to a string literal, allocating 10 bytes of memory space to the P2.
2 The pointer p3 through the function memcpy directly to the memory pointed to by the pointer P2, that is to say, the pointer P2, P3 point to the same memory. Then print the memory value that p2,p3 points to, and the result is the same.
3 finally according to good habits to release P2, and the P3 also set to NULL is to prevent again access to P3 point of memory, resulting in the occurrence of wild pointers.