memset
Function Usage
Set data of a specified length in the destination memory to a specified value.
Function Syntax
void *memset(void *destination, int value, size_t size);
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
destination |
Pointer to the destination memory used to store the reset data. |
The value cannot be NULL. |
Output |
value |
Target value. |
The value is not limited. |
Input |
size |
Number of bytes to be reset. |
The value cannot be negative. |
Input |
Return Value
- Success: A pointer to the destination memory is returned.
- Failure: A null pointer is returned.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include "kqmalloc.h" #define KQMALLOC_TEST_LEN 10 void MemsetExample() { int8_t *destination = (int8_t *)malloc(KQMALLOC_TEST_LEN * sizeof(int8_t)); if (destination == NULL) { printf("destination is null\n"); return; } else { printf("destination address: %lx\n", destination); for (int8_t i = 0; i < KQMALLOC_TEST_LEN; ++i) { destination[i] = i; } printf("destination test data before memcpy:"); for (int8_t i = 0; i < KQMALLOC_TEST_LEN; ++i) { printf(" %d", destination[i]); } printf("\n"); } int8_t *res = memset(destination , 1, KQMALLOC_TEST_LEN * sizeof(int8_t)); printf("res address: %lx\n", res); if (res != NULL) { printf("res test data after memcpy:"); for (int8_t i = 0; i < KQMALLOC_TEST_LEN; ++i) { printf(" %d", res[i]); } printf("\n"); } free(destination); } int main(void) { MemsetExample(); return 0; } |
Output:
1 2 3 4 | destination address: ffff9fe17174 destination test data before memcpy: 0 1 2 3 4 5 6 7 8 9 res address: ffff9fe17174 res test data after memcpy: 1 1 1 1 1 1 1 1 1 1 |
Parent topic: Function Syntax