why use memset and what does memset(a,0,size of(a)); represents?can some one make me clear abt memset with some examples?thanks ..
memset
Collapse
X
-
Originally posted by swatwhy use memset and what does memset(a,0,size of(a)); represents?can some one make me clear abt memset with some examples?thanks ..
If you are going to declare say a array dianamically... . after allocating memory by using malloc or calloc... U can use the memset function to intialise the elements of array with null....
(a,0,sizeof(a)) ;
It syas that a is array and you are going to initailse the elements with null....
For example
Code:#include<stdio.h> int main() { char *s; s=(char*) malloc(20); memset(s,'\0',20); printf("Enter your name\n"); gets(s); printf("your name is\n"); puts(s); return 0; }
-
"The Standard C library function memset( ) (in <cstring>) is used for convenience .... It sets all memory starting at a particular address (the first argument) to a particular value (the second argument) for n bytes past the starting address (n is the third argument). Of course, you could have simply used a loop to iterate through all the memory, but memset( ) is available, well-tested (so it’s less likely you’ll introduce an error), and probably more efficient than if you coded it by hand."
From Thinking in c++Comment
Comment