Loading, please wait...

Memory Functions

The string-handling library functions presented in this section manipulate, compare and search blocks of memory. The functions treat blocks of memory as character arrays and can manipulate any block of data.

 

Following tables summarize the memory functions of the string-handling library. In the function discussions, “object” refers to a block of data.

 

Function Prototype      

Function description

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

Copies n characters from the object pointed to by s2 into the object pointed to by s1. A pointer to the resulting object is returned.

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

Copies n characters from the object pointed to by s2 into the object pointed to by s1. The copy is performed as if the characters were

first copied from the object pointed to by s2 into a temporary array and then from the temporary array into the object pointed to by s1. A pointer to the resulting object is returned.

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

Compares the first n characters of the objects pointed to by s1 and

s2. The function returns 0, less than 0 or greater than 0 if s1 is equal

to, less than or greater than s2.

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

Locates the first occurrence of c (converted to unsigned char) in the

first n characters of the object pointed to by s. If c is found, a pointer to c in the object is returned. Otherwise, NULL is returned.

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

Copies c (converted to unsigned char) into the first n characters of

the object pointed to by s. A pointer to the result is returned.

 

 

Function memcpy

Function memcpy copies a specified number of characters from the object pointed to by its second argument into the object pointed to by its first argument. The function can receive a pointer to any type of object. The result of this function is undefined if the two objects overlap in memory (i.e., if they are parts of the same object) in such cases, use memmove. The example uses memcpy  to copy the string in array s2 to array s1.

 

Example:

/* how to use memcpy */

#include <stdio.h>
#include <string.h>

int main( void )
{
char s1[ 17 ]; /* create char array s1 */
char s2[] = "Copy this string"; /* initialize char array s2 */

memcpy( s1, s2, 17 );
printf( "%s\n%s\"%s\"\n",
"After s2 is copied into s1 with memcpy,",
"s1 contains ", s1 );

return 0;
}

Output:

After s2 is copied into s1 with memcpy,

s1 contains "Copy this string"

 

 

Function memmove

Function memmove, like memcpy, copies a specified number of bytes from the object pointed to by its second argument into the object pointed to by its first argument. Copying is performed as if the bytes were copied from the second argument into a temporary character array, then copied from the temporary array into the first argument. This allows characters from one part of a string to be copied into another part of the same string. Example uses memmove to copy the last 10 bytes of array x into the first 10 bytes of array x.

 

Example:

/*how to use memmove*/

#include <stdio.h>
#include <string.h>

int main( void )
{
char x[] = "Welcome Home Welcome"; /* initialize char array x */

printf( "%s%s\n", "The string in array x before memmove is: ", x );
printf( "%s%s\n", "The string in array x after memmove is: ",
memmove( x, &x[ 5 ], 10 ) );

return 0; /* indicates successful termination */
}

Output:

The string in array x before memmove is: Welcome  Home Welcome

The string in array x after memmove is:  Home Welcome Welcome

 

 

Function memcmp

Function memcmp compares the specified number of characters of its first argument with the corresponding characters of its second argument. The function returns a value greater than 0 if the first argument is greater than the second, returns 0 if the arguments are equal and returns a value less than 0 if the first argument is less than the second.

 

Example:

/*how to use memcmp*/

#include <stdio.h>
#include <string.h>

int main( void )
{
char s1[] = "ABCDEFG"; /* initialize char array s1 */
char s2[] = "ABCDXYZ"; /* initialize char array s2 */

printf( "%s%s\n%s%s\n\n%s%2d\n%s%2d\n%s%2d\n",
"s1 = ", s1, "s2 = ", s2,
"memcmp( s1, s2, 4 ) = ", memcmp( s1, s2, 4),
"memcmp( s1, s2, 7 ) = ", memcmp( s1, s2, 7),
"memcmp( s1, s2, 7 ) = ", memcmp( s1, s2, 7),
return 0;
}

Output:       

s1 = ABCDEFG

s2 = ABCDXYZ



memcmp( s1, s2, 4 ) = 0

memcmp( s1, s2, 7 ) = -1

memcmp( s2, s1, 7 ) = 1

 

 

Function memchr

Function memchr searches for the first occurrence of a byte, represented as unsigned char, in the specified number of bytes of an object. If the byte is found, a pointer to the byte in the object is returned; otherwise, a NULL pointer is returned. Example searches for the character (byte) 'r' in the string "This is a string".

 

Example:

/*how to use memchr*/

#include <stdio.h>
#include <string.h>

int main( void )
{
const char *s = "This is a string"; /* initialize char pointer */

printf( "%s\'%c\'%s\"%s\"\n",
"The remainder of s after character ", 'r',
" is found is ", memchr(s, ‘r’,16) );
return 0;
}

Output:

The remainder of s after character 'r' is found is "ring"

 

 

Function memset

Function memset copies the value of the byte in its second argument into the first n bytes of the object pointed to by its first argument, where n is specified by the third argument. The example uses memset to copy 'b' into the first 7 bytes of string1.

 

Example:

/*how to use memset*/

#include <stdio.h>
#include <string.h>

int main( void )
{
char string1[ 15 ] = "BBBBBBBBBBBBBB"; /* initialize string1 */

printf( "string1 = %s\n", string1 );
printf( "string1 after memset = %s\n", memset( string1, ‘b’, 7) );

return 0;
}

Output:

string1 = BBBBBBBBBBBBBB

string1 after memset = bbbbbbbBBBBBBB