The memset() Function
To set all the bytes in a block of memory to a particular value, use memset(). The function prototype is
void * memset(void
*dest
, int
c
, size_t
count
);
The argument dest points to the block of memory. c is the value to set, and count is the number of bytes, starting at dest, to be set. Note that while c is a type int, it is treated as a type char. In other words, only the low-order byte is used, and you can specify values of c only in the range 0 through 255.
Use memset() to initialize a block of memory to a specified value. Because this function can use only a type char as the initialization value, it is not useful for working with blocks of data types other than type char, except when you want to initialize to 0. In other words, it wouldn’t be efficient to use memset() to initialize an array of type int to the value 99, but you could initialize all array elements to the value 0.
The memcpy() Function
memcpy() copies bytes of data between memory blocks, sometimes called buffers. This function doesn’t care about the type of data being copied–it simply makes an exact byte-for-byte copy. The function prototype is
void *memcpy(void *dest, void *src, size_t count);
The arguments dest and src point to the destination and source memory blocks, respectively. count specifies the number of bytes to be copied. The return value is dest. If the two blocks of memory overlap, the function might not operate properly–some of the data in src might be overwritten before being copied.
The memmove() Function
memmove() is very much like memcpy(), copying a specified number of bytes from one memory block to another. It’s more flexible, however, because it can handle overlapping memory blocks properly. Because memmove() can do everything memcpy() can do with the added flexibility of dealing with overlapping blocks. The prototype is
void *memmove(void *dest, void *src, size_t count);
dest and src point to the destination and source memory blocks, and count specifies the number of bytes to be copied. The return value is dest. If the blocks overlap, this function ensures that the source data in the overlapped region is copied before being overwritten.