Helpful Information
 
 
Category: C Programming
Returning an array with a function

I am taking in an array with a function. When I am done with the function I want to return another array, can I do this?

Thanks,
Jonathan Donaghe

I think the only way to do it is to pass in the array as a pointer or reference and let the function do its thing. I don't think you can explicitly return an array.

So you could have your function take in both arrays in the arguments, or (I'm not sure about this one), have it return the address or a pointer to a new array which was created on the heap.

When you take in the array, and modify it, you can return a new one by returning the pointer.

The passed in parameter should use the pointer syntax, and you should return the new array as a pointer.

Make sure you allocate your new array on the heap using malloc, if you attempt to return a pointer to a static array declared in the function, your program will most likely dump core.

If you go Lord MJ's route, make sure you deallocate the array when you're finished with it, too.

Just for kicks:

Since it's legal (according to Peter van der Linden's "Expert C Programming" book) to return a struct, you -can- return entire arrays, if you put them in a struct like this:



struct mystruct {
int vector[20];
}

struct mystruct foo()
{
struct mystruct bar;

...do something nifty with bar...

return bar;
}


It's kind of a kludge though, and it you can't use it to handle dynamically-sized arrays. YMMV.

You can simply pass array base address in a function and return its base address in a pointer .

Function Declaration::
int *pass(int[],int);


Function call::
int *y;
y=pass(arr,size);


Function definition ::
int *pass(int a[],int s)
{
//Perform any thing you want to perform on array...
return a;
}
:)

Just for kicks:

Since it's legal (according to Peter van der Linden's "Expert C Programming" book) to return a struct, you -can- return entire arrays, if you put them in a struct like this:



struct mystruct {
int vector[20];
}

struct mystruct foo()
{
struct mystruct bar;

...do something nifty with bar...

return bar;
}


It's kind of a kludge though, and it you can't use it to handle dynamically-sized arrays. YMMV.

i am having the same problem this does not work for me

You can simply pass array base address in a function and return its base address in a pointer .

Function Declaration::
int *pass(int[],int);


Function call::
int *y;
y=pass(arr,size);


Function definition ::
int *pass(int a[],int s)
{
//Perform any thing you want to perform on array...
return a;
}
:)
so if i define the following
int*pass(int a[],int s)
{
a[0]=1;
a[1]=2
return a;
}

now the pointer has the memory location of a[0] and a[1]

how can i read their values from main for example?

so if i define the following
int*pass(int a[],int s)
{
a[0]=1;
a[1]=2
return a;
}

now the pointer has the memory location of a[0] and a[1]

how can i read their values from main for example?

simply using returned pointer of array a.For example using *(y) and *(y+1) to access a[0] and a[1].

#include <stdio.h>
#define NUM 3

int *addarrays(int x_array[], int y_array[]);

int main(){

int a_array[NUM] = {1, 2, 3};
int b_array[NUM] = {1, 2, 3};
int counter, *ptr;

ptr = addarrays(a_array, b_array);

for(counter = 0; counter < NUM; counter++){
printf("\nThird array %d element is: %d", counter + 1, *ptr++);
}

return 0;
}

int *addarrays(int x_array[], int y_array[]){

int z_array[NUM], count = 0;

for(count; count < NUM; count++){
z_array[count] = x_array[count] + y_array[count];
}

return &z_array[0];

}


Hi guys, i have the above code trying to return an array from a function addarrays()

However, the element in third_array are 0, 3618984, 10105482

It seem to be pointing correctly to the first element of the returned array. For the 2nd element and 3rd element, the value is wrong.

Does anyone knows what I am short of? :confused: :confused:

1.Go and create your own thread. You shouldn't wake up old threads like this.

2. Read this (http://forums.devshed.com/c-programming-42/new-users-how-to-post-a-question-read-this-first-259106.html). Always post your codes in code tags. Don't use Quote tags for that purpose, it doesn't preserve the indentation.


3. The problem with your code is that you are storing your result in a temporary variable z_array, which is known only to the function addarrays(). I've posted a revised code, understand it first. And if any doubt persists, open a new thread to discuss it.


#include <stdio.h>
#define NUM 3

void addarrays(int x_array[], int y_array[],int sum_array[]);

int main()
{
int a_array[NUM] = {1, 2, 3};
int b_array[NUM] = {1, 2, 3};
int sum_array[NUM] = {0, 0, 0};
int counter, *ptr=sum_array;
addarrays(a_array, b_array,sum_array);
for(counter = 0; counter < NUM; counter++)
{
printf("\nThird array %d element is: %d", counter + 1, *ptr++);
}
return 0;
}

void addarrays(int x_array[], int y_array[], int sum_array[])
{
int count = 0;
for(;count<NUM; count++)
{
sum_array[count] = x_array[count] + y_array[count];
}
}

Happy coding :)










privacy (GDPR)