Reputation: 579
I need my function to return an array, but it doesn't take an array as argument as most of search examples show.
The code is like this:
double myfunction ()
{
double arr[10];
//assign values to the array
return arr;
}
main()
{
double arr2[10];
arr2[10] = myfunction;
//print arr2
}
When I used pointers to display the array, I got values like "CCCCCC"...
Upvotes: 1
Views: 6057
Reputation: 1
You can't return an built-in array or a function from a function. In other words, the return type of a function can't be a built-in array or a function type.
But you can return a pointer to either of those.
Other option is to use std::array
or std::vector
as the return type.
One example using std::array
is shown below:
std::array<int, 5> func()
{
std::array<int, 5> localArray{1,2,3,4,5};
//do something with array
//return it
return localArray;
}
Upvotes: 0
Reputation: 10083
Example returning a std::vector
.
#include <vector>
std::vector <int> some_numbers()
{
return { 2, 3, 5, 7, 11, 13, 17, 19 };
}
Obviously you can assign values to the vector any way you wish. This example just returns a hard-coded list of primes.
You can use it anywhere any container for which begin
and end
work as usual:
#include <iostream>
int main()
{
for (int x : some_numbers())
std::cout << x << "\n";
}
Upvotes: 0
Reputation: 145419
Note: main
must have result type int
, by both the C and the C++ standards.
It's also a good idea to use const
just about everywhere that it’s possible, because that adds constraints to the code which makes the code easier to understand (less to consider).
And finally, and most important, do use the standard library facilities.
#include <array>
using namespace std;
typedef array<double, 10> MyArray;
MyArray myfunction ()
{
MyArray arr;
//assign values to the array
return arr;
}
int main()
{
MyArray const arr = myfunction();
//print arr2
}
Upvotes: 1
Reputation: 32540
You can't do that ... arrays are blocks of memory, and thus can't be "returned" ... You can pass pointers to arrays around through function calls, but you can't safely return pointers that are pointing to array memory blocks unless those arrays are declared static
. Otherwise you will get undefined results as the memory on the stack occupied by the array will be reclaimed when the function returns, and the returned pointer to the stack memory location will be invalid. Using the returned pointer will most likely corrupt the stack, or crash your program.
In C you would have to explicitly use a pointer to a heap-allocated array in order to avoid this issue, but with C++ the best thing to use would be std::vector
or std::array
for a fixed-size array. For instance, std::vector
still uses heap memory to store the array (not the stack), but via RAII the heap memory is safely managed by the object's lifetime. Therefore unlike C you won't have to worry about memory leaks from failing to free the memory pointed to by pointers returned from functions that are pointing to heap memory.
Upvotes: 6