0
  #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?

Jérôme
  • 1,254
  • 2
  • 20
  • 25
Gab
  • 1
  • Keep a back up before passing to function. – Steephen Mar 12 '17 at 03:17
  • Try this mental excersize: substitute a simple variable, say `int x`, for your array. Everywhere you currently have an array, you just have an `int x`, and you would be passing a pointer to the `int x`, instead of an array. Now, answer your own question. You should be able to answer it easily. If not, it's time for you reread the last 2 or 3 chapters in your C++ book. But if you can answer this simplified question, then you should now be able to easily realize that the same exact answer that you came up for `int x` also applies to an array as well. Wonders never cease! – Sam Varshavchik Mar 12 '17 at 03:24
  • You are actually asking question how to pass array by value http://stackoverflow.com/questions/4774456/pass-an-array-to-a-function-by-value – PapaDiHatti Mar 12 '17 at 03:32

1 Answers1

1

You cannot pass array contents by value so to retain original values you need to maintain copy of original array as described in below code :

 #include <iostream>
 #include <cstring>
  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 ];  
int arraycopy[sizeof(array2)];
std::copy(array2,array2+3,arraycopy);
   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]) << "  ";
std::copy(arraycopy,arraycopy+3,array2);
} 
PapaDiHatti
  • 1,841
  • 19
  • 26