How to Use memset(),memcpy(),memmove() in C

      memset() function is used to set all the bytes in a block memory to particular value.Generally this is used to set a memory location to null character '\0'.

               void *memset(void *dest,int c,size_t count);        
               dest: Pointer to the block of memory.
               c: value to be set.c is type int,but it is treated as type char.It is not useful for working           with blocks of data types other than type char,except when you want to initialize to 0.
               count: Number of bytes set to the value.


      memcpy() function is used to copies bytes of data between memory blocks.
This will copy "count" characters from the string "src" into the memory pointed to by  "dest".

               void *memcpy(void *dest,void *src,size_t count);  

               dest:destination string

               src: source string
               count: Number of bytes to copy.

Unlike strncpy function, memcpy() function does not check for the terminating '\0' of dest.
memcpy() can not handle the situation where the source string overlaps destination string.


       move block of memory.

               void *memmove(void *dest,void *src,size_t count);  

               dest:destination string
               src: source string

               count: Number of bytes to copy.

memmove() can safely handle the overlap situation.

No comments:

Post a Comment