1

I'm trying to copy the contents of array1 to array2 using a self-written function called ArrayCopy(). The arrays are both the same size. I have to use pointers to do it.

I know that the integers in array1 are copying over to array2, but when I print array2 outside of the ArrayCopy() function, it still prints with all 0s. I know array1 is successfully getting values from cin.

I cannot change the block of code at the end of main(), otherwise I will lose points on the assignment.

Part of ArrayCopy() is commented out, but it's just something I tried that didn't work.

For testing, the values I've entered for array1 are 1, 2, 3, 4, 5.

#include <iostream>
#include <iomanip>
#include <array>

const int ARRAYSIZE = 5;

void ArrayCopy(std::array<int, ARRAYSIZE>, std::array<int, ARRAYSIZE>);

int main() {

    std::array<int, ARRAYSIZE> array1;
    std::array<int, ARRAYSIZE> array2 = {};
    
    std::cout << "Please input " << ARRAYSIZE << " integers: ";

    // Get user input into array1

    for (int i = 0; i < ARRAYSIZE; i++) {

        std::cin >> array1[i];

        }

  // DO NOT CHANGE THIS CODE
    std::cout << "\n\nBefore copy, array 1 is: \n";
    PrintArray(array1);
    std::cout << "\nAnd array 2 is: \n";
    PrintArray(array2, ARRAYSIZE);

    ArrayCopy(array1, array2);

    std::cout << "\n\nAfter copy, array 1 is: \n";
    PrintArray(array1);
    std::cout << "\nAnd array 2 is: \n";
    PrintArray(array2, ARRAYSIZE);

   // END CODE THAT YOU SHOULD NOT CHANGE

}

void ArrayCopy(std::array<int, ARRAYSIZE> array1, std::array<int, ARRAYSIZE> array2) {

    for (size_t i = 0; i < array1.size(); i++) {

        int *holder1 = &array1[i];
        int *holder2 = &array2[i];

        array2[i] = *holder1;

        /*
        int* temp = holder2;

        holder2 = holder1;
        holder1 = temp;

        int holderInteger = *holder2;

        std::cout << holderInteger;

        array2[i] = holderInteger;
        */

    }

}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Faraday12
  • 31
  • 5
  • 5
    You need to pass arrays by reference. – Vlad from Moscow Mar 22 '23 at 18:37
  • 2
    What's wrong with `std::copy()` to use in that case?? – πάντα ῥεῖ Mar 22 '23 at 18:38
  • *I'm trying to copy the contents of array1 to array2 using a self-written function* -- Your function has bugs and is slower than what the standard library provides. What's your reason for writing your own function? `std::copy(array1.begin(), array1.end(), array2.begin());` or just `array2 = array1;` – PaulMcKenzie Mar 22 '23 at 18:53
  • Related: [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419/) and [How to pass objects to functions in C++?](https://stackoverflow.com/questions/2139224/) – Remy Lebeau Mar 22 '23 at 19:43

2 Answers2

2

You need to pass arrays by reference as for example

void ArrayCopy( const std::array<int, ARRAYSIZE> &array1, std::array<int, ARRAYSIZE> &array2) {

    for (size_t i = 0; i < array1.size(); i++) {

        const int *holder1 = &array1[i];
        int *holder2 = &array2[i];

        *holder2 = *holder1;
    }
}

Otherwise the function deals with copies of the original arrays.

Pay attention to that you could just assign one array to another like

array2 = array1;

For example

void ArrayCopy( const std::array<int, ARRAYSIZE> &array1, std::array<int, ARRAYSIZE> &array2) {
    array2 = array1;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I can't do that without changing the function call in main. The function call is in the code block that I can't change. I would try to create pointers for the arrays before that, but I'm not sure how. – Faraday12 Mar 22 '23 at 18:51
  • 1
    @Faraday12 The function call will be the same without any changes ArrayCopy(array1, array2); – Vlad from Moscow Mar 22 '23 at 18:53
  • @Faraday12 -- I know it's hard to believe, but simply doing `array2 = array1;` in `ArrayCopy` does everything you were trying to do with all of that code with pointers. You were trying to open the door by pushing on the door, throwing your weight on the door, using sledgehammers, etc. instead of just pulling on the door to open it. – PaulMcKenzie Mar 22 '23 at 18:59
  • @Vlad Thank you so much. I thought by changing the function parameters passed in at the definition would require changing the function call as well, but I guess not. Thanks again. – Faraday12 Mar 22 '23 at 19:11
  • @Faraday12 No at all. You are welcome.:) – Vlad from Moscow Mar 22 '23 at 19:31
0

You pass the array by value. That means when you call ArrayCopy(array1, array2);, a copy of array1 and array2 is passed to the function. Your copying inside the function only copies into the copy of array2.

A solution is to change the type of the parameter the function receives to std::array<int, ARRAYSIZE>&. The & after a typename means it is a reference, so when you call the function a reference to your array is passed to the function and your function modifies the original array.

wediaklup
  • 41
  • 5