C Programming/C Reference/nonstandard/memccpy
(Redirected from C Programming/C Reference/string.h/memccpy)
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
editvoid *memccpy (void *dest, const void *src, int c, size_t n);
Argument Description destIt points to the location of a destination string. srcIt points to the location of a source string. cIt specifies a character for which to search and copy. nIt 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.
See also
edit- memcpy
- memmove
- strcpy
- memcmp
- memset
- strncpy