#include <cstring>
memset(a, 0, sizeof(a));
//memset ( variable name , values , sizeof ( variable name ) );
//
//Memset ( ) function Is used to fill a block of memory with a particular value(0 and -1)
// problem 1.
/*int a[10]={0};
cout<<a[5]<<endl;
int b[10]={21}; // method wrong ... and luckily valid for only 0 (zero )
cout<<b[5]<<endl; */
// problem 2.
/* int n ; cin>>n;
int a[n]={0};
cout<<a[5]<<endl; */
// solution of these problem
// memset ()
int n;
cout<<"Input the size of array"<<endl;
cin>>n;
int a[n]; // block name --- a
memset(a,0 ,sizeof(a)); // value --> 0 & -1
cout<<a[4]<<endl;
int val[9];
memset(val, -1 , sizeof(val));
for(int i=0;i<9;i++)
{
cout<<val[i]<<" ";
}
cout<<endl;
char str[] = "friedchicken";
memset(str, 't', sizeof(str));
cout << str<<endl;
// CPP Program to demonstrate that we can use memset() to
// set all values as 0 or -1 for integral data types also
#include <bits/stdc++.h>
using namespace std;
// Driver Code
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] << " ";
}
memset(array,0,M*N*sizeof (int));
//2d array
/* 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!
/* 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;
}
memset(a, 0, sizeof(a))
memset(str, '#', sizeof(str));
Code Example |
---|
Cpp :: c++ print array of arrays with pointer |
Cpp :: How to pass a multidimensional array to a function in C and C++ |
Cpp :: size of unordered_set |
Cpp :: c++ lambda as variable |
Cpp :: c++ write string |
Cpp :: cpp undefined reference to function |
Cpp :: compare function in c++ |
Cpp :: c++ hash |
Cpp :: queue operations c++ |
Cpp :: if not c++ |
Cpp :: minimum or maximum in array c++ |
Cpp :: insertion overloading in c++ |
Cpp :: dynamic memory in c++ |
Cpp :: c++ function overloading |
Cpp :: cpp compiler online |
Cpp :: c++ visual studio |
Cpp :: char input in c++ |
Cpp :: c++ download |
Cpp :: c++ string replace |
Cpp :: faster solutions |
Cpp :: how-to-read-until-eof-from-cin-in-c++ |
Cpp :: arithmetic progression c++ |
Cpp :: nand in cpp |
Cpp :: c++ Difference Array | Range update query in O(1) |
Cpp :: c++ how to use and or in if |
Cpp :: c++ terinary operator |
Cpp :: C++ Automatic Conversion from double to int |
Cpp :: sento freddo a un dente |
Cpp :: C++ singleton prevent copy |
Cpp :: C++ References |