C memset() function
C memset() function - set bytes in memory
The memset() function is used to set the first n bytes of dest to the value c. The value of c is converted to an unsigned character.
Use memset() function when:
- Initializing arrays or buffers to a default value, particularly before usage.
- Clearing memory by setting all bytes to zero, useful for resetting variables or data structures.
- Setting specific portions of memory to a particular value in a quick and efficient way.
Syntax:
void *memset(void *dest, int c, size_t n)
Parameters:
Name | Description | Required /Optional |
---|---|---|
dest | Pointer to destination. | Required |
c | Character to set. | Required |
n | Number of bytes to be set to the value. | Optional |
Return value from memset()
The value of dest.
Examples: memset() function
Example 1: Sets the first five characters of a buffer string to #.
Following example demonstrates how memset() sets the first five characters of a buffer string to #. It modifies only the beginning of the string, which can be useful for partially masking or formatting data.
Code:
#include <stdio.h>
#include <string.h>
int main( void )
{
char buffer[] = "C programming memset function";
printf( "Before: %s\n", buffer );
memset( buffer, '#', 5 );
printf( "After: %s\n", buffer );
}
Output:
Before: C programming memset function After: #####gramming memset function
Example 2: Sets 10 bytes of the buffer to 1 and the next 10 bytes to 2
Following example initializes a buffer by setting the first 10 bytes to the character '1' and the next 10 bytes to '2'. It highlights how memset() can be used to populate different sections of memory with different values.
Code:
#include <string.h>
#include <stdio.h>
#define BUF_SIZE 20
int main(void)
{
char buffer[BUF_SIZE + 1];
char *string;
memset(buffer, 0, sizeof(buffer));
string = (char *) memset(buffer,'1', 10);
printf("\nBuffer contents: %s\n", string);
memset(buffer+10, '2', 10);
printf("\nBuffer contents: %s\n", buffer);
}
Output:
Buffer contents: 1111111111 Buffer contents: 11111111112222222222
Example 3: Initializing an integer array to zero
This example demonstrates using memset() to initialize all elements of an integer array to zero.
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Declare an integer array
int numbers[10];
// Set all bytes in the array to zero
memset(numbers, 0, sizeof(numbers));
// Display the array contents
printf("Array initialized to zero: ");
for (int i = 0; i < 10; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Output:
Array initialized to zero: 0 0 0 0 0 0 0 0 0 0
Explanation:
- An integer array numbers is declared.
- memset() sets all bytes in numbers to zero, effectively initializing each element to 0.
- The array is then printed to confirm all elements are zero.
Example 4: Setting a structure’s field values to default
This example demonstrates using memset() to initialize all fields of a structure to -1.
Code:
#include <stdio.h>
#include <string.h>
struct Data {
int id;
float score;
char name[20];
};
int main() {
// Declare a structure variable
struct Data data;
// Set all bytes of the structure to -1
memset(&data, -1, sizeof(data));
// Display the structure values
printf("Data after memset:\n");
printf("id: %d, score: %f, name: %s\n", data.id, data.score, data.name);
return 0;
}
Output:
Data after memset: id: -1, score: -1.#QNAN0, name:
Explanation:
- A structure Data with an integer, a float, and a character array is defined and initialized.
- memset() sets all bytes in the structure to -1, filling the fields with default values.
- The structure fields are printed to show that memset() has affected all fields in the structure.
C Programming Code Editor:
Previous C Programming: C memmove()
Next C Programming: C strcat()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming/string/c-memset.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics