Array elements are all initialized to the same value problem

Source: Internet
Author: User
Tags arrays
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;  }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.