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

memset():     
      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'.

               Syntax:
               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():    

      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".

               Syntax:
               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.

memmove():  

       move block of memory.

                Syntax:
               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