I have an array int arr[5]
that is passed to a function fillarr(int arr[])
:
int fillarr(int arr[])
{
for(...);
return arr;
}
- How can I return that array?
- How will I use it, say I returned a pointer how am I going to access it?
I have an array int arr[5]
that is passed to a function fillarr(int arr[])
:
int fillarr(int arr[])
{
for(...);
return arr;
}
Actually when you pass an array inside a function, the pointer to the original array is passed in the function parameter and thus the changes made to the array inside that function is actually made on the original array.
Run it and you will see the changes
C++ functions can't return C-style arrays by value. The closest thing is to return a pointer. Furthermore, an array type in the argument list is simply converted to a pointer.
You can improve it by using an array references for the argument and return, which prevents the decay:
With Boost or C++11, pass-by-reference is only optional and the syntax is less mind-bending:
The
array
template simply generates astruct
containing a C-style array, so you can apply object-oriented semantics yet retain the array's original simplicity.Source: https://www.tutorialspoint.com/cplusplus/cpp_return_arrays_from_functions.htm
C++ does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.
Applying these rules on the current question, we can write the program as follows:
The Output will be:
$8.3.5/8 states-
"Functions shall not have a return type of type array or function, although they may have a return type of type pointer or reference to such things. There shall be no arrays of functions, although there can be arrays of pointers to functions."
the answer may depend a bit on how you plan to use that function. For the simplest answer, lets decide that instead of an array, what you really want is a vector. Vectors are nice because the look for all the world like boring, ordinary values you can store in regular pointers. We'll look at other options and why you want them afterwards:
This will do exactly what you expect it to do. The upside is that
std::vector
takes care of making sure everything is handled cleanly. the downside is that this copies a very large amount of data, if your array is large. In fact it copies every element of the array twice. first it copies the vector so that the function can use it as a parameter. then it copies it again to return it to the caller. If you can handle managing the vector yourself, you can do things quite a bit more easily. (it may copy it a third time if the caller needs to store it in a variable of some sort to do more calculation)It looks like what you're really trying to do is just populate a collection. if you don't have a specific reason to return a new instance of a collection, then don't. we can do it like this
this way you get a reference to the array passed to the function, not a private copy of it. any changes you make to the parameter are seen by the caller. You could return a reference to it if you want, but that's not really a great idea, since it sort of implies that you're getting something different from what you passed.
If you really do need a new instance of the collection, but want to avoid having it on the stack (and all the copying that entails), you need to create some kind of contract for how that instance is handled. the easiest way to do that is to use a smart pointer, which keeps the referenced instance around as long as anyone is holding onto it. It goes away cleanly if it goes out of scope. That would look like this.
For the most part, using
*myArr
works identically to using a plain vanilla vector. This example also modifies the parameter list by adding theconst
keyword. Now you get a reference without copying it, but you can't modify it, so the caller knows it'll be the same as before the function got to it.All of this is swell, but idiomatic c++ rarely works with collections as a whole. More normally, you will be using iterators over those collections. that would look something more like this
Using it looks a bit odd if you're not used to seeing this style.
foo now 'points to' the beginning of the modified
arr
.What's really nice about this is that it works equally well on vector as on plain C arrays and many other types of collection, for example
Which now looks an awful lot like the plain pointer examples given elsewhere in this question.