memset c++
// CPP program to illustrate memset
//works with var adresses ,basically characters but limit its use to -1 and 0 in int
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char str[] = "geeksforgeeks";
memset(str, 't', sizeof(str));
cout << str;
return 0;
}
output:
tttttttttttttt
#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] << " ";
}
#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] << " ";
}