0
Q:

memset c

#include <bits/stdc++.h> 
using namespace std; 
   
int main() 
{ 
    int a[5]; 
   
    // all elements of A are zero 
    memset(a, 0, sizeof(a)); 
    for (int i = 0; i < 5; i++) 
        cout << a[i] << " "; 
    cout << endl; 
   
    // all elements of A are -1 
    memset(a, -1, sizeof(a)); 
    for (int i = 0; i < 5; i++) 
        cout << a[i] << " "; 
    cout << endl; 
   
    // Would not work 
    memset(a, 5, sizeof(a)); // WRONG 
    for (int i = 0; i < 5; i++) 
        cout << a[i] << " "; 
} 
3
/* memset example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "almost every programmer should know memset!";
  memset (str,'-',6);
  puts (str);
  return 0;
}

// Output:
// ------ every programmer should know memset!
3
0 0 0 0 0 
-1 -1 -1 -1 -1 
84215045 84215045 84215045 84215045 84215045
1
// memset syntax
void * memset ( void * ptr, int value, size_t num );
1
void *memset(void *str, int c, size_t n)
0
This is string.h library function
$$$$$$$ string.h library function
0
#include <stdio.h>
#include <string.h>

int main () {
   char str[50];

   strcpy(str,"This is string.h library function");
   puts(str);

   memset(str,'$',7);
   puts(str);
   
   return(0);
}
0

New to Communities?

Join the community