memset() in C with examples - GeeksforGeeks

28 Jun, 2021
memset() is used to fill a block of memory with a particular value.The syntax of memset() function is as follows :
// ptr ==> Starting address of memory to be filled
// x   ==> Value to be filled
// n   ==> Number of bytes to be filled starting 
//         from ptr to be filled
void *memset(void *ptr, int x, size_t n);

Note that ptr is a void pointer, so that we can pass any type of pointer to this function.
Let us see a simple example in C to demonstrate how memset() function is used:
// C program to demonstrate working of memset()#include <stdio.h>#include <string.h>  int main(){    char str[50] = "GeeksForGeeks is for programming geeks.";    printf("\nBefore memset(): %s\n", str);      // Fill 8 characters starting from str[13] with '.'    memset(str + 13, '.', 8*sizeof(char));      printf("After memset():  %s", str);    return 0;}
Output:
Before memset(): GeeksForGeeks is for programming geeks.
After memset(): GeeksForGeeks........programming geeks.

Explanation: (str + 13) points to first space (0 based index) of the string “GeeksForGeeks is for programming geeks.”, and memset() sets the character ‘.’ starting from first ‘ ‘ of the string up to 8 character positions of the given string and hence we get the output as shown above.
// C program to demonstrate working of memset()#include <stdio.h>#include <string.h>  void printArray(int arr[], int n){   for (int i=0; i<n; i++)      printf("%d ", arr[i]);}  int main(){    int n = 10;    int arr[n];      // Fill whole array with 0.    memset(arr, 0, n*sizeof(arr[0]));    printf("Array after memset()\n");    printArray(arr, n);      return 0;}
Output:
0 0 0 0 0 0 0 0 0 0
Exercise :Predict the output of below program.
// C program to demonstrate working of memset()#include <stdio.h>#include <string.h>  void printArray(int arr[], int n){   for (int i=0; i<n; i++)      printf("%d ", arr[i]);}  int main(){    int n = 10;    int arr[n];      // Fill whole array with 100.    memset(arr, 10, n*sizeof(arr[0]));    printf("Array after memset()\n");    printArray(arr, n);      return 0;}
Note that the above code doesn’t set array values to 10 as memset works character by character and an integer contains more than one bytes (or characters).
However, if we replace 10 with -1, we get -1 values. Because representation of -1 contains all 1s in case of both char and int.
Reference: memset man page (linux)
This article is contributed by MAZHAR IMAM KHAN. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
abhnv
CPP-Library
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects
Bitwise Operators in C/C++
Virtual Function in C++
Templates in C++ with Examples
Constructors in C++
Operator Overloading in C++
Socket Programming in C/C++
Object Oriented Programming in C++