One: Initialize with memset
Two: Direct initialization when defining data types
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
int a[10],b[10],c[10];
BOOL MAP[10][10];
memset (A,0x3f,sizeof (a));
a[2]=0x3f3f3f3f;
memset (map,false,sizeof (map));//correct
memset (b,0,sizeof (b));
Memset (C,-1,sizeof (c));
Memset (C,2,sizeof (c)); Error cannot initialize array to 2
int d[10]={0};//Although the array element is all 0, it is not an int that is obtained by all initialization to zero
e[10]={-1};// Unable to initialize all arrays to-1;
cout<<a[0]<< ' <<a[1]<<endl;
cout<<a[2]<<endl;
cout<<b[0]<< ' <<b[1]<<endl;
cout<<c[0]<< ' <<c[1]<<endl;
cout<<d[0]<< ' <<d[1]<<endl;
cout<<e[0]<< ' <<e[1]<<endl;
}
Three: Default values for arrays not initialized #include <iostream> using namespace std; int a[10]; char b[10]; Int main () { for ( int i = 0; i < 10; i++) //int Global Array uninitialized defaults to 0 { cout << a[i]; } cout << endl; int c[10]; for (int i = 0; i < 10; i + +) //Local array is not initialized, random value { cout << c[i]; &nbsP } cout << endl; int d[10] = { 0 }; for (int i = 0; i < 10; i++) //is 0, an element that is not explicitly initialized defaults to 0, and the character array is ' + ' { cout << d[i]; } cout << endl; cout << b << endl; //is output as a string, the output is empty, not a space, You can see that the uninitialized global array element is ' + ' for (int i = 0; i < 10; i++) //single output is all a { cout << b[i]; } cout << endl; char e[10] = { ' B ' }; cout << e << endl; The //is output as a string, the output is B, and the element that is not explicitly initialized is ' \ 0 ' for (int i = 0; i < 10; i++) //char local array, single element outputs result of baaaaaaaaa { cout << e[i]; } cout << endl; char f[10]; //char Local Array not initialized , the output is hot hot pressing for (int i = 0; i < 10; i++) { cout << f[i]; } cout << endl; return 0; }