memset
Function Usage
Populates a value into each byte of a memory block. It is usually used to initialize or clear the memory block.
Function Syntax
void *memset(void *ptr, int value, size_t num);
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
ptr |
Pointer to the destination memory used to store the reset data. |
Non-null pointer to a valid memory block, with at least num bytes accessible. |
Input/Output |
value |
Target value, which will be converted into the unsigned char type. |
0 to 255. The excess part will be truncated. |
Input |
num |
Number of bytes to be reset. |
Non-negative number that does not exceed the size of the memory block pointed to by ptr. |
Input |
Return Value
- Success: A pointer to the destination memory is returned. The return value is generally ignored.
- Failure: See those of open source glibc. No other exception values will be returned.
This function is used to initialize or clear a memory block by filling it with 0 or another value on a byte-by-byte basis. Therefore, the size of the value should be taken into account.
Example
#include <stdio.h>
#include <string.h>
int main() {
char byteArr[10];
memset(byteArr, 'A', sizeof(byteArr));
for(int i = 0; i < sizeof(byteArr); i++) {
printf("%c ", byteArr[i]);
}
printf("\n");
return 0;
}
Output:
A A A A A A A A A A
Parent topic: Function Syntax