Memccpy, memmove, memcpy, memset, menchr, memcmp, and memicmp

Source: Internet
Author: User
Several common memory processing functions are listed here for simple introduction and comparison.

Memccpy

 prototype: extern void * memccpy (void * DEST, void * SRC, unsigned char CH, unsigned int count ); 

usage: # include
function: no more than count bytes are copied from the memory area indicated by Src to the memory area indicated by DeST. If the CH character is encountered, the replication is stopped.
note: the pointer to the first character after the CH character is returned. If ch does not exist in the First n Bytes of SRC, null is returned. Ch is copied.
example:

// memccpy. c
# include
# include

main ()
{< br> char * s = "golden Global View";
char d [20], * P;
clrscr ();
P = memccpy (D, S, 'x', strlen (s ));
If (p)
{< br> * P = '\ 0'; // must do this
printf ("char found: % S. \ n ", d);
}< br> else
printf (" char not found. \ n ");


getchar ();
return 0;
}

Memcpy

Prototype: extern void * memcpy (void * DEST, void * SRC, unsigned int count );

 

Usage: # include <string. h>

Function: copy count bytes from the memory area indicated by Src to the memory area indicated by DeST.

Note: the memory areas specified by Src and DEST cannot overlap. The function returns a pointer to DeST.

Example:

// Memcpy. c

# Include <syslib. h>
# Include <string. h>

Main ()
{
Char * s = "golden Global View ";
Char d [20];

Clrscr ();

Memcpy (D, S, strlen (s ));
D [strlen (s)] = 0;
Printf ("% s", d );

Getchar ();
Return 0;
}

Memmove

Prototype: extern void * memmove (void * DEST, const void * SRC, unsigned int count );

Usage: # include <string. h>

Function: copy count bytes from the memory area indicated by Src to the memory area indicated by DeST.

Note: SRC and DEST indicate that the memory areas can overlap, but the SRC content will be changed after replication. The function returns a pointer to DeST.

Example:

 


// Memmove. c

# Include <syslib. h>
# Include <string. h>

Main ()
{
Char * s = "golden Global View ";

Clrscr ();

Memmove (S, S + 7, strlen (S)-7 );
S [strlen (S)-7] = 0;
Printf ("% s", S );

Getchar ();
Return 0;
}

Memset

Prototype: extern void * memset (void * buffer, int C, int count );

Usage: # include <string. h>

Function: sets the first count byte of the memory area referred to by buffer to character C.

Note: The pointer to the buffer is returned.

Example:

 


// Memset. c

# Include <syslib. h>
# Include <string. h>

Main ()
{
Char * s = "golden Global View ";

Clrscr ();

Memset (S, 'G', 6 );
Printf ("% s", S );

Getchar ();
Return 0;
}

Memchr

Prototype: extern void * memchr (void * Buf, char CH, unsigned count );

 

Usage: # include <string. h>

Function: searches for the character ch from the first count bytes of the memory area specified by BUF.

Note: When the first ch character is encountered, stop searching. If the call succeeds, a pointer to the CH character is returned; otherwise, null is returned.

Example:

// Memchr. c

# Include <syslib. h>
# Include <string. h>

Main ()
{
Char * s = "Hello, programmers! ";
Char * P;

Clrscr ();

P = memchr (S, 'P', strlen (s ));
If (P)
Printf ("% s", P );
Else
Printf ("not found! ");

Getchar ();
Return 0;
}

Memcmp

 prototype: extern int memcmp (void * buf1, void * buf2, unsigned int count); 
usage: # include
function: Compares the first count bytes of buf1 and buf2 in the memory area.
Note:
when buf1 when buf1 = buf2, return Value = 0
when buf1> buf2, return value> 0
example:

// memcmp. c
# include
# include

main ()
{< br> char * S1 = "Hello, programmers! ";
char * S2 =" Hello, programmers! ";
int R;
clrscr ();
r = memcmp (S1, S2, strlen (S1 ));
If (! R)
printf ("S1 and S2 are identical");
else
If (r <0)
printf ("S1 less than S2");
else
printf ("S1 greater than S2 ");

getchar ();
return 0;
}

Memicmp

 prototype: extern int memicmp (void * buf1, void * buf2, unsigned int count); 
usage: # include
function: Compares the first count bytes of the buf1 and buf2 in the memory region, but does not distinguish uppercase and lowercase letters.
note: the only difference between memicmp and memcmp is that memicmp is case-insensitive.
when buf1 when buf1 = buf2, return value = 0
when buf1> buf2, return value> 0
example:

// memicmp. c
# include
# include

main ()
{< br> char * S1 = "Hello, programmers! ";
char * S2 =" Hello, programmers! ";
int R;
clrscr ();
r = memicmp (S1, S2, strlen (S1 ));
If (! R)
printf ("S1 and S2 are identical");
else
If (r <0)
printf ("S1 less than S2");
else
printf ("S1 greater than S2 ");

getchar ();
return 0;
}

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.