Skip to main content

Returning array from a function

Return arrays from function

A function can not return more than one value thus it is not possible to directly return arrays as values from a function in C.

However, you can use the following methods to indirectly return an array from a function:

  • Return a pointer: You can return a pointer to an array. This is the most common way to return an array from a function.

  • Pass a pointer as an argument: You can pass a pointer as an argument to the function and modify the elements of the array using the pointer.

Return Array using pointer

Here's an example to return Array from a function using pointer

#include <stdio.h>

int *getArray(int size)
{
static int arr[100];
for (int i = 0; i < size; i++)
{
arr[i] = i * 2;
}
return arr;
}

int main(void)
{
int *p, size = 5;
p = getArray(size);
for (int i = 0; i < size; i++)
{
printf("%d ", *(p + i));
}
return 0;
}
Output:

Explanation:

  • The getArray function returns a pointer to an array arr.
  • The arr array is declared as a static array, which means its contents persist even after the function call is over.
  • The getArray function assigns values to the elements of the array and returns its address, which is stored in the pointer p.
  • The values of the array can be accessed using the pointer p.
tip

Note that you have to dereference the pointer using the * operator to access the values stored in the array.

Return Array using argument

You can also return an array from a function by passing an argument that acts as an output parameter.

Here's an example:

#include <stdio.h>

void getArray(int size, int arr[])
{
for (int i = 0; i < size; i++)
{
arr[i] = i * 2;
}
}

int main(void)
{
int arr[100], size = 5;
getArray(size, arr);
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Output:

Explanation:

  • The getArray function takes two arguments: size and arr.
  • The arr argument acts as an output parameter and the function assigns values to the elements of the array.
  • The main function declares an array arr and calls the getArray function, passing the size of the array and the array itself.
  • The getArray function assigns values to the elements of the array, which can be accessed in the main function after the function call is over.

Modify Array using argument

Here is another example that allows us to pass a pointer to the array to the function and then use that pointer to modify the contents of the array.

#include <stdio.h>

void modifyArray(int *arr, int size)
{
for (int i = 0; i < size; i++)
{
arr[i] = arr[i] + 1;
}
}

int main(void)
{
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
modifyArray(arr, size);
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Output:

Explanation:

  • The modifyArray function takes a pointer to an array arr and its size size as arguments.
  • The contents of the array are modified in the function and the changes are reflected back in the caller function.