Memory Block Management

The following functions declared in string.h are used to compare, search, or initialize memory buffers:

void *memchr( const void *buf, int c, size_t n );

Searches the first n bytes of the buffer buf for the first occurrence of the character c.

void *memcmp( const void *s1, const void *s2, size_t n );

Compares the first n bytes in the buffer s1 with the corresponding bytes in the buffer s2. The return value is less than, equal to, or greater than 0 to indicate whether s1 is less than, equal to, or greater than s2.

void *memcpy( void *dest, const void *src, size_t n );

Copies n bytes from the buffer src to the buffer dest.

void *memmove( void *dest, const void *src, size_t n );

Copies n bytes from the buffer src to the buffer dest. In case the buffers overlap, every character is read before another character is written to the same location.

void *memset( void *dest, int c, size_t n );

Fills the first n bytes of the buffer dest with the character c.

The corresponding wmem... functions, for handling buffers of wide characters with type wchar_t, are declared in the header file wchar.h (*).

Get C Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.