Passing arrays and individual array elements to functions : array « Array « C++ Tutorial






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

#include <iomanip>
using std::setw;

void modifyArray( int [], int ); // appears strange
void modifyElement( int );

int main()
{
   const int arraySize = 5; // size of array a
   int a[ arraySize ] = { 0, 1, 2, 3, 4 }; // initialize array a

   modifyArray( a, arraySize );  

   for ( int i = 0; i < arraySize; i++ )
      cout << setw( 3 ) << a[ i ];

   modifyElement( a[ 3 ] );
   cout << a[ 3 ] << endl;

   return 0;
}

void modifyArray( int b[], int sizeOfArray )
{
   for ( int i = 0; i < sizeOfArray; i++ )
      b[ i ] = 200;
}

void modifyElement( int e )
{
   e = 200 ;
}
200200200200200200








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