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?
In this case, your array variable
Is kind of just syntactic sugar. You could really replace it with this and it would still work:
So in the same sense, what you want to return from your function is actually a pointer to the first element in the array:
And you'll still be able to use it just like you would a normal array:
|
|||||||||||||||||||
|
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 |
|||||||||
|
This:
is actually treated the same as:
Now if you really want to return an array you can change that line to
It's not really returning an array. you're returning a pointer to the start of the array address. But remember when you pass in the array, you're only passing in a pointer. So when you modify the array data, you're actually modifying the data that the pointer is pointing at. Therefore before you passed in the array, you must realise that you already have on the outside the modified result. e.g.
I suggest you might want to consider putting a length into your fillarr function like this.
That way you can use length to fill the array to it's length no matter what it is. To actually use it properly. Do something like this:
If all you're wanting to do is set the array to some default values, consider using the built in memset function. something like: memset((int*)&arr, 5, sizeof(int)); While I'm on the topic though. You say you're using C++. Have a look at using stl vectors. Your code is likely to be more robust. There are lots of tutorials. Here is one that gives you an idea of how to use them. http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html |
|||
|
$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 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 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 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. |
||||
|
to return an array from a function , let us define that array in a structure; So it looks something like this
Now let us create variables of the type structure.
We can pass array to a function in the following way and assign value to it:
We can also return the array. To return the array , the return type of the function should be of structure type ie marks. This is because in reality we are passing the structure that contains the array. So the final code may look like this.
|
|||
|
You can still use the result like
|
|||||
|
std::vector
. – GManNickG Aug 13 '10 at 3:33