What is the memset command used for C. is it used for setting fixed memory locations. I am making a wild guess from the name. Someone correct me if I am wrong. Also tell me what header files I have to include while using this command.
Printable View
What is the memset command used for C. is it used for setting fixed memory locations. I am making a wild guess from the name. Someone correct me if I am wrong. Also tell me what header files I have to include while using this command.
The memset is mainly used to initialize or set memory section with some value.
The syntax of memset is
void *memset( void *buffer, int c, count );
In this memset copies the value of c into the first count characters of buffer, and returns buffer. The header file that must be included while using this function is <string.h>
The declaration of memset function is as follows
[B]void* memset(void* s, int c, int n); [/B]
the memset function sets the first n bytes in s to c.
[B]Generally this function is used to set a memory location to null chracters '\0' [/B]The memset function returns s with the value set to c
For example consider the code
#include<string.h>
#include<stdio.h>
void main()
{
char str[]="geekinterview";
printf("length before using memset =%d",strlen(str));
memset(str,'\0',sizeof(str));
printf("\nlength after using memset =%d",strlen(str)); getch(); }
[B]output [/B]
length before using memset 13
length after using memset 0