Static arrays are initialized to zero. : array « Array « C++ Tutorial






#include <iostream>
using std::cout;
using std::endl;

void staticArrayInit( void );
void automaticArrayInit( void );

int main()
{
   staticArrayInit();
   automaticArrayInit();

   staticArrayInit();
   automaticArrayInit();
   return 0;
}

void staticArrayInit( void )
{
   static int array1[ 3 ];

   for ( int i = 0; i < 3; i++ )
      cout << "array1[" << i << "] = " << array1[ i ] << "  ";

   for ( int j = 0; j < 3; j++ )
      array1[ j ] = 0;
}

void automaticArrayInit( void )
{
   int array2[ 3 ] = { 1, 2, 3 };

   for ( int i = 0; i < 3; i++ )
      cout << "array2[" << i << "] = " << array2[ i ] << "  ";

   for ( int j = 0; j < 3; j++ )
      array2[ j ] = 0;
}
array1[0] = 0  array1[1] = 0  array1[2] = 0  array2[0] = 1  array2[1] = 2  array
2[2] = 3  array1[0] = 0  array1[1] = 0  array1[2] = 0  array2[0] = 1  array2[1]
= 2  array2[2] = 3








4.1.array
4.1.1.Initializing an array
4.1.2.Initializing an array in a declaration.
4.1.3.Static arrays are initialized to zero.
4.1.4.Passing arrays and individual array elements to functions
4.1.5.Linear search of an array
4.1.6.Use subscripting and pointer notations with arrays
4.1.7.array of strings
4.1.8.Obtaining the number of array elements