Comparison of the use of memccpy () functions and memcpy () functions in C language _c language

Source: Internet
Author: User

C language memccpy () function: copying content in memory
header file:

#include <string.h>

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:

memccpy (): string (b)

C language memcpy () function: Copy memory content (ignore)
header file:

#include <string.h>

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:

P2 = abcde
p3 = ABCDE

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.

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.