VOOZH about

URL: https://www.geeksforgeeks.org/c/return-an-array-in-c/

⇱ Return an Array in C - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Return an Array in C

Last Updated : 23 Jul, 2025

In C, arrays are linear data structures that allow users to store the same data in consecutive memory locations. Returning an array in C can be a little complex because unlike C++, C does not support directly returning arrays from functions. In this article, we will learn how to return an array in C.

Here, we will discuss each of the above methods one by one with the help of example.

Return an Array in C Using Pointers

One of the most common ways to return an from a in C is by using . This involves allocating memory for the array within the function and then returning a pointer to this memory.

Syntax

To return an array using pointer follow the below syntax:

data_type* arr = (data_type*)malloc(n * sizeof(data_type));

Here,

  • arr is the name of the pointer pointing to the array.
  • n is the size of the array.
  • data_type denotes the type of the data present in the array.

C Program to Return an Array in C Using Pointers

The below program demonstrates how we can return an array from a function in C using the using pointers.


Output
Array Elements: 0 2 4 6 8 

Time Complexity: O(N), where N denotes the returned array size.
Auxiliary Space: O(N)

Return an Array in C Using Static Arrays

Another approach to return an array in C is using the . Once an array is declared static it's lifetime extends beyond the function scopes. The static keyword allows the array to retain its values between different function calls. So we can declare a pointer in the main function to get hold of the static array easily.

Syntax

static data_type arr[n]

Here,

  • data_type denotes the type of the data that the static array will store.
  • arr is the name of the static array.
  • n denotes the size of the static array.

Note: You can return the array name from a function and use a pointer to get hold of the static array wherever you want to use it.

C Program to Return an Array in C Using Static Arrays

The following program demonstrates how we can return an array from a function in C using the using static arrays.


Output
Array Elements: 0 2 4 6 8 

Time Complexity: O(N), where N denotes the returned array size.
Auxiliary Space: O(1), as the array is static once it's size is declared it can't be changed further so it remains constant.

Return an Array in C Using Structures

In C we can also use structures to encapsulate arrays and then we can easily return them from functions. Following is the syntax to return an array using structures in C.

Syntax

struct Struct_name{
data_type arr[n]
};

Here,

  • Struct_name is the name of the structure used to hold the array.
  • data_type is the type of the data present in the array.
  • n denotes the size of the array.

Note: To return the array we can return the whole structure from the function and then access the array using the dot operator. We can also return multiple arrays by storing them in a single structure.

C Program to Return an Array in C Using Structures

The following program demonstrates how we can return an array from a function in C using the using structures.


Output
Array Elements: 0 2 4 6 8 

Time Complexity: O(N), where N denotes the returned array size.
Auxiliary Space: O(1), as the size of the array is constant.

Comment