3

I'm relatively new to C++ and am having a tough time passing my array into a separate function. Apologies for re-asking a question that has no doubt been answered a dozen times before, but I couldn't find any questions similar to the problem I have with my code.

int main()
{
    Array<int> intarray(10);
    int grow_size = 0;

    intarray[0] = 42;
    intarray[1] = 12;
    intarray[9] = 88;

    intarray.Resize(intarray.Size()+2);
    intarray.Insert(10, 6);

    addToArray(intarray);

    int i = intarray[0];

    for (i=0;i<intarray.Size();i++) 
    cout<<i<<'\t'<<intarray[i]<<endl;

    Sleep(5000);
}

void addToArray(Array<int> intarray)
{
    int newValue;
    int newIndex;

    cout<<"What do you want to add to the array?"<<endl;
    cin >> newValue;
    cout<<"At what point should this value be added?"<<endl;
    cin >> newIndex;

    intarray.Insert(newValue, newIndex);
}
7
  • 4
    What is the question? Commented Jun 28, 2013 at 15:13
  • You need to provide the Array::Insert implementation if you want someone to help you. Commented Jun 28, 2013 at 15:15
  • @JamesMcLaughlin It is new in C++11 cplusplus.com/reference/array/array Commented Jun 28, 2013 at 15:16
  • 1
    @stonemetal That's std::array, not Array as used in this program. Commented Jun 28, 2013 at 15:17
  • Ahh, I shouldn't have deleted my comment, then. So what is this mysterious Array? Commented Jun 28, 2013 at 15:17

3 Answers 3

5

You are passing a copy of the array, so any changes will not affect the original. Pass by reference instead:

void addToArray(Array<int> &intarray)
//                         ^
Sign up to request clarification or add additional context in comments.

5 Comments

That's not his only problem though.
@0x499602D2 Care to elaborate?
He declared an object but he's using it as an array: Array<int> intarray(10). That should be Array<int> intarray[10]
@0x499602D2 I'm not sure that's true; the constructor could be Array::Array(size_t size), which seems reasonable. Without seeing Array.h though, I couldn't say for sure.
Thanks, that solved the problem I was having. I also realized that I had forgotten to declare the function. It seems to be working fine now.
2

This is a special case of a more general question on parameters passing.

You may want to consider the following guidelines:

  1. If you want to pass something to a function to modify it inside the function (and make the changes visible to the caller), pass by reference (&).

    e.g.

    // 'a' and 'b' are modified inside function's body,
    // and the modifications should be visible to the caller.
    //
    //     ---> Pass 'a' and 'b' by reference (&) 
    //
    void Swap(int& a, int& b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
    
  2. If you want to pass something that is cheap to copy (e.g. an int, a double, etc.) to a function to observe it inside the function, you can simply pass by value.

    e.g.

    // 'side' is an input parameter, "observed" by the function.
    // Moreover, it's cheap to copy, so pass by value. 
    //
    inline double AreaOfSquare(double side)
    {
        return side*side;
    }
    
  3. If you want to pass something that is not cheap to copy (e.g. a std::string, std::vector, etc.) to a function to observe it inside the function (without modifying it), you can pass by const reference (const &).

    e.g.

    // 'data' is an input parameter, "observed" by the function.
    // It is in general not cheap to copy (the vector can store
    // hundreds or thousands of values), so pass by const reference.
    //
    double AverageOfValues(const std::vector<double> & data)
    {
        if (data.empty())
            throw std::invalid_argument("Data vector is empty.");
    
        double sum = data[0];
        for (size_t i = 1; i < data.size(); ++i)
            sum += data[i];
    
        return sum / data.size();
    }
    
  4. In modern C++11/14 there is also an additional rule (related to move semantics): if you want to pass something that is cheap to move and make a local copy of it, then pass by value and std::move from the value.

    e.g.

    // 'std::vector' is cheap to move, and the function needs a local copy of it.
    // So: pass by value, and std::move from the value.
    //
    std::vector<double> Negate(std::vector<double> v)
    {
        std::vector<double> result( std::move(v) );
        for (auto & x : result)
            x *= -1;
        return result;
    }
    

Since in your addToArray() function you modify the Array<int> argument, and you want modifications visible to the caller, you can apply rule #1, and pass by reference (&):

void addToArray(Array<int> & intarray)

Comments

-1

My suggestion is use pointer, as usual declare the function

void yourFunction(int * arrayPointer);

In main() function you should input

int yourArray[8] = {11, -2, 45, 37, 18, 35};
int * arrayPointer = &yourFunction[0];
yourFunction(yourArray); // call your function

Finally in yourFunction()

void yourFunction(int * arrayPointer)
{
    // print all number in the array
    for(int x = 0; x < 6; x++)
    {
        cout << *arrayPointer << " ";
        arrayPointer++;
    }
}

This at least work for me, hope this will help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.