Return Array in a Function

Return array in a function

In this case, your array variable arr can actually also be treated as a pointer to the beginning of your array's block in memory, by an implicit conversion. This syntax that you're using:

int fillarr(int arr[])

Is kind of just syntactic sugar. You could really replace it with this and it would still work:

int fillarr(int* arr)

So in the same sense, what you want to return from your function is actually a pointer to the first element in the array:

int* fillarr(int arr[])

And you'll still be able to use it just like you would a normal array:

int main()
{
int y[10];
int *a = fillarr(y);
cout << a[0] << endl;
}

How can you return an array from a function in c++

Try to pass the array as a reference instead of returning it.

#include "iostream"

void range(int max, int* ranger){
int n = 0;
while (n < max){
ranger[n] = n;
n += 1;
}

}

int main(){
int ranger[5];
range(5, ranger);
for(int k = 0; k < 5; k++){
std::cout << ranger[k] << " ";
}
return 0;
}

Edit: As @Jarod42 pointed out, I've changed the memory allocation from int * to int[5].

How to return an array from a function?

int* test();

but it would be "more C++" to use vectors:

std::vector< int > test();

EDIT

I'll clarify some point. Since you mentioned C++, I'll go with new[] and delete[] operators, but it's the same with malloc/free.

In the first case, you'll write something like:

int* test() {
return new int[size_needed];
}

but it's not a nice idea because your function's client doesn't really know the size of the array you are returning, although the client can safely deallocate it with a call to delete[].

int* theArray = test();
for (size_t i; i < ???; ++i) { // I don't know what is the array size!
// ...
}
delete[] theArray; // ok.

A better signature would be this one:

int* test(size_t& arraySize) {
array_size = 10;
return new int[array_size];
}

And your client code would now be:

size_t theSize = 0;
int* theArray = test(theSize);
for (size_t i; i < theSize; ++i) { // now I can safely iterate the array
// ...
}
delete[] theArray; // still ok.

Since this is C++, std::vector<T> is a widely-used solution:

std::vector<int> test() {
std::vector<int> vector(10);
return vector;
}

Now you don't have to call delete[], since it will be handled by the object, and you can safely iterate it with:

std::vector<int> v = test();
std::vector<int>::iterator it = v.begin();
for (; it != v.end(); ++it) {
// do your things
}

which is easier and safer.

Returning an array using C

You can't return arrays from functions in C. You also can't (shouldn't) do this:

char *returnArray(char array []){
char returned [10];
//methods to pull values from array, interpret them, and then create new array
return &(returned[0]); //is this correct?
}

returned is created with automatic storage duration and references to it will become invalid once it leaves its declaring scope, i.e., when the function returns.

You will need to dynamically allocate the memory inside of the function or fill a preallocated buffer provided by the caller.

Option 1:

dynamically allocate the memory inside of the function (caller responsible for deallocating ret)

char *foo(int count) {
char *ret = malloc(count);
if(!ret)
return NULL;

for(int i = 0; i < count; ++i)
ret[i] = i;

return ret;
}

Call it like so:

int main() {
char *p = foo(10);
if(p) {
// do stuff with p
free(p);
}

return 0;
}

Option 2:

fill a preallocated buffer provided by the caller (caller allocates buf and passes to the function)

void foo(char *buf, int count) {
for(int i = 0; i < count; ++i)
buf[i] = i;
}

And call it like so:

int main() {
char arr[10] = {0};
foo(arr, 10);
// No need to deallocate because we allocated
// arr with automatic storage duration.
// If we had dynamically allocated it
// (i.e. malloc or some variant) then we
// would need to call free(arr)
}

How do I return an array of struct from a function?

struct Operator fun()
{
struct Operador items[3];
...
return items[n];
}

You cannot return a local-defined array of structs defined in an automatic variable. And what you do in your case, you return items[n] for an n where items was not initialized.

you can return an array only if you allocate it on the heap and you can do something so:

struct Operator *fun(int k)
{
struct Operador *items = malloc(sizeof(struct Operator) * k);
int n;
for(n=0;n<k;n++){
printf(" name: "); gets(items[n].nome);
printf(" telefone: "); gets(items[n].telefone);
printf(" age: "); gets(items[n].idade);
}
return items;
}


Related Topics



Leave a reply



Submit