HOWTO · Arduino
Arduino memset() Function
One can set the bytes of a value to destination using the memset() function in Arduino.
On this page
memset() fills a specified number of bytes from a destination address with one repeated byte value. Use sizeof to calculate the byte count for an array, and use typed assignment when you need to set numeric elements to a value.
Introduction to the memset() Function in Arduino
In Arduino C++, memset() fills a consecutive byte range with one repeated byte value. It is useful for byte buffers and character arrays; it does not copy a multi-byte value into each element. The third argument is a count of bytes, so calculate it from the storage you intend to fill.
The standard function is declared by <string.h> (also available through the C++ header <cstring>). Its form is memset(destination, value, byte_count): destination points to the first byte, value is converted to an unsigned byte, and byte_count says how many bytes to write. The function repeats that converted byte; it does not take bytes from the original value.
Use the memset() Function in Arduino
This complete C++ example fills a character array and then changes a range within it. It uses standard output so the byte operation can be demonstrated without a board-specific serial connection.
#include <cstring>
#include <iostream>
int main() {
char letters[] = "hello";
std::memset(letters, '.', sizeof(letters) - 1);
std::memset(letters + 1, 'a', 2);
std::cout << letters << '\n';
return 0;
}
.aa..
The array has room for the five letters and the terminating null character. The first call fills only the five visible character bytes, leaving the terminator in place. The second starts at index 1 and writes two bytes, so it changes indices 1 and 2. On an Arduino sketch, the same array operations work with memset(); use Serial.print() to display the resulting character array.
For an array with a different element type, derive the byte count from the array itself. For example, memset(values, 0, sizeof(values)) fills every byte in values. To fill a range of whole elements, advance by an element count and multiply by that element’s size: memset(values + start, byte_value, count * sizeof(values[0])). Array indices start at zero, and the selected count must remain within the array.
Filling an integer array with byte value 2 does not assign integer value 2 to each element. On a target with 16-bit little-endian int, each pair of bytes becomes 0x0202, which represents 514. Other integer widths or representations can produce a different result. Use a loop such as for (int i = 0; i < count; ++i) values[i] = 2; when the goal is to assign the typed value 2 to each element.
To change characters in Arduino’s String, assign through its indexed characters, for example text[i] = '.';. That is direct character assignment, not a use of memset().
Important Considerations
Always ensure the destination points to writable storage and that the byte count fits within that storage. For an array, sizeof(array) gives the total byte size only where the expression still refers to the array itself; a function parameter declared as an array is a pointer, so pass or calculate its length separately. A zero byte count writes no bytes, but the destination must still be a valid pointer under the function’s contract.
A repeated byte pattern is not a general way to assign values to objects. It is suitable when that byte-level representation is intended, such as filling a character buffer or clearing bytes. Avoid using it to initialize non-trivial C++ objects, including objects that manage resources or rely on constructors and invariants. Use typed assignment, value initialization, or the object’s own API for ordinary values and objects.
Conclusion
Use memset() when you need to repeat one byte across a known byte range. Size the range in bytes, account for element width when working with arrays, and keep every write within the destination’s storage. For integer values, strings, and objects, prefer typed assignment or the appropriate initialization method unless a repeated byte pattern is specifically what you need.