C Programming/C Reference/nonstandard/memccpy

From Wikibooks, open books for an open world
Jump to: navigation, search

The Memccpy function ( which stands for "copy bytes in memory" ) is mainly a function of C Standard Library, typically associated with some type of programming languages.This function operate on strings in memory areas. The memccpy function shall copy bytes from one memory area to other, stopping after the first occurrence of some byte X (converted to an Unsigned char) or after n bytes are copied, whichever comes first.

Syntax[edit]

 void *memccpy (void *dest, const void *src, int c, size_t n);

Argument Description
dest
It points to the location of a destination string.
src
It points to the location of a source string.
c
It specifies a character for which to search and copy.
n
It specifies the number of characters to be copied.

In above syntax, size_t is a typedef. It is unsigned data type defined in stddef.h.

Description[edit]

Here memccpy function shall copy bytes from the memory area src into dest, stopping after the first occurrence of byte c, or after n bytes are copied, whichever comes first. Here character c is also copied.


Returns[edit]

The memccpy function returns a pointer to the next character in dest after c or NULL if c was not found in the first n characters of src.


Examples[edit]

 
#include <memory.h>
#include <stdio.h>
#include <string.h>
 
char string1[60] = "Taj Mahal is a historic monument in India.";
 
int main( void ) {
 
   char buffer[61];
   char *pdest;
   printf( "Function: _memccpy 42 characters or to character 'c'\n" );
   printf( "Source: %s\n", string1 );
   pdest = _memccpy( buffer, string1, 'c', 42);
   *pdest = '\0';
   printf( "Result: %s\n", buffer );
   printf( "Length: %d characters\n", strlen( buffer ) );
}
Output
Function: _memccpy 42 characters or to character 'c'
Source: Taj Mahal is a historic monument in India.
Result: Taj Mahal is a historic
Length: 23 characters




#include <stdio.h>
#include <string.h>
 
char *msg = "This is the string: not copied";
 
void main() {
 
    char buffer[80];
    memset( buffer, '\0', 80 );
    memccpy( buffer, msg, ':', 80 );
    printf( "%s\n", buffer );
  }
Output
This is the string:



Application Usage[edit]

The memccpy function does not check for the overflow of the receiving memory area.

www.pubs.opengroup.org
www.kernel.org
www.sandia.gov

See also[edit]