#include <iostream>
using std::cout;
using std::endl;
void staticArrayInit(int[]);
int main()
{
int array2[3]={1,2,3};
cout << "First call to each function:\n";
staticArrayInit(array2);
cout << "\n\nSecond call to each function:\n";
staticArrayInit(array2);
cout << endl;
return 0;
}
void staticArrayInit(int array2[])
{
static int array1[ 3 ];
cout << "\nValues on entering staticArrayInit:\n";
for ( int i = 0; i < 3; i++ )
cout << "array1[" << i << "] = " << array1[ i ] << " ";
cout << "\nValues on exiting staticArrayInit:\n";
for ( int j = 0; j < 3; j++ )
cout << "array1[" << j << "] = "
<< ( array1[ j ] += 5 ) << " ";
cout << "\n\nValues on entering automaticArrayInit:\n";
for ( int i = 0; i < 3; i++ )
cout << "array2[" << i << "] = " << array2[ i ] << " ";
cout << "\nValues on exiting automaticArrayInit:\n";
for ( int j = 0; j < 3; j++ )
cout << "array2[" << j << "] = "
<< (array2[ j ] += array1[j]) << " ";
}
As you can see, staticarrayinit
is gonna be called twice. After the first call, the original values of array2
(1,2,3) will be modified and in the second call the values that will be displayed are the modified ones. How can I retain the original values of array2
and display it in the second call of staticArrayInit
?