Return an Array in C

In C, functions cannot directly return arrays, as arrays are not first-class citizens. However, there are several ways to simulate returning an array from a function, such as returning a pointer to the array or passing the array to the function by reference.

Definition

Although C does not allow functions to directly return an array, a function can return a pointer to an array or use a pointer parameter to modify an array passed by the caller. The key thing to remember is that arrays in C decay into pointers when passed to functions.

Syntax:

                        
return_type* function_name(parameters);
                        
                    

Method 1: Returning a Pointer to a Static Array

One way to return an array from a function is to return a pointer to a statically allocated array. Since the array exists in the memory location after the function call, its memory is not lost after returning from the function.

Example 1: Returning a Pointer to a Static Array

                        
#include <stdio.h>
int* createArray() 
{
    static int arr[5] = {1, 2, 3, 4, 5}; // Static array
    return arr; // Returning a pointer to the array
}
        
int main() 
{
    int* ptr = createArray();
        
    // Print the elements of the returned array
    for (int i = 0; i < 5; i++) {
        printf("Element %d: %d\n", i, ptr[i]);
    }
        
            return 0;
}
                        
                    

Output

Element 0: 1
Element 1: 2
Element 2: 3
Element 3: 4
Element 4: 5

Method 2: Passing an Array by Reference

Another method is to pass an array to the function by reference, which allows the function to modify the contents of the array. In this case, the function doesn't return the array but modifies the original array passed by the caller.

Example 2: Passing an Array by Reference

                        
#include <stdio.h>
void fillArray(int arr[], int size) 
{
    for (int i = 0; i < size; i++) {
    arr[i] = i + 1; // Modifying the array elements
    }
}
        
int main() 
{
    int arr[5];
    fillArray(arr, 5);
        
    // Print the modified array
    for (int i = 0; i < 5; i++) {
    printf("Element %d: %d\n", i, arr[i]);
    }
    return 0;
}
                        
                    

Output

Element 0: 1
Element 1: 2
Element 2: 3
Element 3: 4
Element 4: 5

Key Points

Method 3: Returning a Dynamically Allocated Array

In some cases, you may need to return a dynamically allocated array from a function. This requires using memory allocation functions like `malloc` to allocate memory on the heap, and the caller is responsible for freeing the memory once it's done using it.

Example 3: Returning a Dynamically Allocated Array

                        
#include <stdlib.h>
        
int* createDynamicArray(int size) {
int* arr = (int*)malloc(size * sizeof(int)); // Dynamically allocated array
   for (int i = 0; i < size; i++) {
       arr[i] = i + 1;
   }
   return arr; // Returning the pointer to the array
  }
        
int main() 
{
    int* arr = createDynamicArray(5);
        
    // Print the elements of the returned array
    for (int i = 0; i < 5; i++) {
        printf("Element %d: %d\n", i, arr[i]);
    }
        
    free(arr); // Free the dynamically allocated memory
    return 0;
}
                        
                    

Output

Element 0: 1
Element 1: 2
Element 2: 3
Element 3: 4
Element 4: 5