Hi! I'm a "kind-of beginner" to C++ programming. I know all of the basics and some "semi-advanced" topics. However, I am having difficulty with returning arrays from functions. I do not know how to accomplish this, and I am having difficulty understanding what I find online. Could anyone please try explaining how to accomplish this, in a semi-basic way?
ARRAYS which are passed as function paramenter are not passwed by value, they are passed by reference!
there is no copy of array for function maded on the stack. that mean fuction will directly work with an passed array! (by reference)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
void function (short MyArray [10]) { // declare a function which takes array as parameter
for (short i = 0; i < 10; i++)
MyArray [i] = 0;
}
int main () {
short SomeArray [10];
for (short i = 0; i < 10; i++)
SomeArray [i] = i *= i; //initialize array with some numers ( i * i )
function (SomeArray); // this function call will delete all member values
return 0;
}
array as function parameter is allway passwd by reference and newer by value.
while with variables you can choose how to pass them ( by values, references or pointers) this is not the case with arrays.
You can return a pointer to the array but if you return an array of an automatic array created within the function you'll get segmentation faults or some weird behaviour when you try to access the returned array
1 2 3 4 5 6
// wrong - compiles but won't work
int* foo ()
{
int arr[5];
return arr; // arr will be destructed
}
You can return a pointer to a dynamic array ( that you have to manually allocate/free )
1 2 3 4 5 6 7 8 9 10
// correct but if not used carefully may lead to errors or memory leaks
int* foo ()
{
int arr = newint[5]; // allocate the array
return arr; // arr must be destructed manually
}
//...
int *bar = foo(); // get the array
// use the array here
delete[] bar; // free the array
A vector does the same as the above but it manages memory for you:
However, Bazzy, I am now trying to use vectors, however, I formerly had arrays. I replaced my array references with references to the vector, and I declared the vector, however, I am trying to declare the vector inside of a specific function, and then return that vector so that I can use its values somewhere else.
I'm not sure what data type I should declare the function as so it can allow console output/user input to the vector (this vector needs to accept characters, as either a char or string type), and then have the function output the vector's values and then return the vector values for use in a different function.
Unfortunately, I can't seem to get this vector to work (do references to indexes work the same as with an array?), and one error says that it is not part of the member, "std."
You need to #include<vector>
references to vector elements will be valid unless you call a function which may change the vector size.
To access a vector element you need to have a vector with the required size.
More info on vectors at: http://cplusplus.com/vector