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);
}
3 Answers 3
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)
// ^
5 Comments
Array<int> intarray(10). That should be Array<int> intarray[10]Array::Array(size_t size), which seems reasonable. Without seeing Array.h though, I couldn't say for sure.This is a special case of a more general question on parameters passing.
You may want to consider the following guidelines:
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; }If you want to pass something that is cheap to copy (e.g. an
int, adouble, 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; }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(); }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::movefrom 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
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
Explore related questions
See similar questions with these tags.
Array::Insertimplementation if you want someone to help you.std::array, notArrayas used in this program.Array?