In C, the memset() function is used to set a one-byte value to a memory block byte by byte. This function is useful for initialization of a memory block byte by byte by a particular value. In this article, we will see in detail how this function can be used. So, let’s get started.
Header File:
1
|
string.h
|
Syntax:
1
|
This function sets the first n bytes of the memory block pointed by str by ch.
Arguments:
The function takes 3 arguments:
- str: This is the pointer of the memory location where the memory will be set. This is a void pointer, so we can set any type of memory block, but the memory will be set byte by byte.
- ch: This is the value that is to be copied to the memory block. This is an integer value, but it is converted to an unsigned character before copied.
- n: This is the number of bytes in the memory block which is set.
Return values:
memset() returns the first address of the memory block from where it starts to set the value.
Examples:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
In Example1.c, we have declared one character array of size 30. Then we have initialized it with the string “ABCD EFGH.” In the memset function, we have passed 3 arguments str, ‘x’ and 3. So, the memory block pointed by str will be reset the first 3 characters by ‘x.’ After memset, when we print the memory, we will get “xxxD EFGH.”
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
In Example2.c, we have passed str+4 to memset function. So, it reset the memory after the 4th location of str. After memset, when we print the memory, we will get “ABCDxxxGH.”
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
In Example3.c, we have declared an integer array of size 5 and trying to initialize it by 10. But from the output, we have seen that the array is not initialized by 10; instead, we have got the value “168430090”. This is because the integer value is greater than one byte and the memset function converts the value to an unsigned character before copied. Now, we will see how we will get the value “168430090”.
The binary representation of 10 is 00000000 00000000 00000000 00001010.
When integer converted to unsigned char, the lower 1 byte is considered. So, when 10 is converted to unsigned char, it’s a binary representation is 00001010.
memset() function sets the memory location byte by byte. So, a total of 4 bytes will be: 00001010 00001010 00001010 00001010.
The decimal value of the binary representation of 4 bytes is 168430090.
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
In Example4.c, we have initialized the integer array by 0. All bits of the binary representation of 0 is 0. So the array is initialized by 0.
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
In Example5.c, we have initialized the integer array by 0. All bits of the binary representation of -1 is 1. So the array is initialized by -1.
Conclusion:
In this article, we have seen using the memset function how we can initialize or set the value of a memory block efficiently. We can set any character and 0 or -1 as an integer value to a memory block. Memset function is faster to set a large chunk of contiguous memory in comparison with simply setting the location using a loop.
from Linux Hint https://ift.tt/2IHTMFt
0 Comments