memset

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swat
    New Member
    • Nov 2006
    • 7

    memset

    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 ..
  • Manjiri
    New Member
    • Nov 2006
    • 40

    #2
    Originally posted by swat
    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 ..

    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;
    }
    Hope you are satisfied with ans...

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      "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

      • rsr
        New Member
        • Dec 2006
        • 25

        #4
        thanks for all the replies

        Comment

        Working...