Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Memset in C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 458
    Comment on it

    Memset is used to fill first n bytes in the block of memory pointed by a pointer to a specific character interpreted as an unsigned char. Memset is defined under string.h header.

    Function Prototype

    void * memset ( void * ptr, int value, size_t num );
    

    About the parameters
    ptr
    ptr is the pointer to the block of memory
    value
    the value to be filled
    num
    number of bytes to be filled

    Sample program using memset

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
            char c[] = "Findnerd";
    
            printf("Before applying memset : %s\n",c);
    
            memset(c,'+',4);
    
            printf("After applying memset  : %s\n",c);
            return 0;
    }
    

    Output of the above program

    Before applying memset : Findnerd
    After applying memset  : ++++nerd
    

    In case you declare an integer array of size n with name arr and you want to fill all the locations with 0, then you can use memset instead of writing a separate code to set all the locations to 0. Use the following syntax for this purpose.
                              memset(arr,0,sizeof(arr));

    This will set all memory locations in integer array arr to 0.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: