Back to C Programming
2026-07-146 min read

C - Return Array from Function

Learn C - Return Array from Function step by step with clear examples and exercises.

Title: Return Array from Function in C - full guide with Worked Examples and Common Mistakes

Why This Matters

In C programming, functions are a fundamental building block for creating efficient and modular code. One common requirement is to return an array from a function, which can be useful when we want to perform operations on arrays without cluttering the main program. Understanding how to do this correctly is essential for writing clean and effective C programs.

Prerequisites

Before diving into returning arrays from functions in C, you should have a solid understanding of:

  1. Basic data types (int, char, etc.)
  2. Arrays and their properties
  3. Functions and function prototypes
  4. Passing arguments by value and reference
  5. Pointers and memory allocation
  6. Understanding the difference between stack and heap memory
  7. Dynamic memory allocation using malloc() and free()

Core Concept

To return an array from a function in C, we cannot directly use the return keyword since arrays are not objects that can be returned like integers or floats. Instead, we pass a pointer to the array as a parameter and modify it within the function. Let's break down the process:

  1. Define the function with a prototype, taking an array as a parameter and passing it by reference using the * symbol.
void myFunction(int arr[], int size);
  1. Inside the function, we can modify the contents of the array using the pointer.
void myFunction(int arr[], int size) {
// Perform operations on arr[] here
}
  1. To return the modified array, we pass a pre-allocated array as an argument to the function and modify it within the function. In the main program, we allocate memory for the array dynamically using malloc().
int main() {
int arrSize = 5;
int* arr = (int*)malloc(arrSize * sizeof(int));

myFunction(arr, arrSize);

// Now we can use the modified array in our main program
}
  1. In the function, we can fill the array and return using void, since the purpose of this example is to demonstrate returning an array. However, for a more practical implementation, we would typically return a pointer to the first element of the array.
int* myFunction(int arr[], int size) {
// Perform operations on arr[] here and return a pointer to the modified array
}

Worked Example

Let's create a function that returns a sorted array using bubble sort. We will pass the array and its size to the function, modify it within the function, and finally return a pointer to the sorted array.

#include <stdio.h>

void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}

int* bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}

return arr;
}

int main() {
int arr[] = {5, 3, 8, 4, 2};
int size = sizeof(arr) / sizeof(arr[0]);
int* sortedArr = bubbleSort(arr, size);

printf("Sorted array: ");
for (int i = 0; i < size; ++i) {
printf("%d ", sortedArr[i]);
}
printf("\n");

free(sortedArr); // Don't forget to deallocate memory!

return 0;
}

Common Mistakes

  1. Forgetting to pass the array size as a separate argument:

Incorrect:

void myFunction(int arr[]) { ... }

Correct:

void myFunction(int arr[], int size) { ... }
  1. Not allocating memory for the array in the main program:

Incorrect:

int main() {
myFunction(arr, arrSize); // arr is not defined!
}

Correct:

int main() {
int* arr = (int*)malloc(arrSize * sizeof(int));
myFunction(arr, arrSize);
}
  1. Not using a pointer to modify the array in the function:

Incorrect:

void myFunction(int arr[]) { ... }

Correct:

void myFunction(int arr[], int size) { ... }
  1. Not deallocating memory after using the returned array:

Incorrect:

int* sortedArr = bubbleSort(arr, size);
// Use the sorted array here...

Correct:

int* sortedArr = bubbleSort(arr, size);
// Use the sorted array here...
free(sortedArr); // Don't forget to deallocate memory!

Practice Questions

  1. Write a function that returns the sum of all elements in an array.
  2. Write a function that finds the average of all elements in an array.
  3. Write a function that reverses the order of elements in an array.
  4. Write a function that finds the second largest number in an array.
  5. Write a function that returns the index of the maximum element in an array.
  6. Write a function that multiplies each element of an array by a given scalar value.
  7. Write a function that concatenates two arrays and returns the resulting array.
  8. Write a function that duplicates an array and returns the duplicate.
  9. Write a function that finds all unique elements in an array.
  10. Write a function that sorts an array using quicksort.

FAQ

Why do we pass the size of the array as a separate argument instead of using sizeof(arr) / sizeof(arr[0]) within the function?

Passing the size as a separate argument allows the function to be more flexible, as it can handle arrays of different sizes without modifying the main program. Using sizeof(arr) / sizeof(arr[0]) would force the array to have a fixed size and make the function less reusable.

What happens if we forget to free the memory allocated for the array in the main program?

If we forget to call free() on the dynamically allocated array, a memory leak occurs, which can lead to performance issues or program crashes. Always remember to deallocate memory when it's no longer needed.

Can we return an array from a function without using pointers?

No, since arrays are not objects that can be returned like integers or floats, we must use pointers to pass and modify the array within the function.

Why do we use malloc() instead of calloc() when allocating memory for an array?

Both malloc() and calloc() can be used to allocate memory dynamically, but their behavior differs slightly. calloc() initializes the allocated memory to zero, while malloc() does not. In most cases, we prefer using malloc(), as it is more flexible and allows us to initialize the array elements later in our code.

Why do we use stack memory instead of heap memory for local variables within a function?

Local variables within a function are typically stored on the stack because they have a known, fixed size, making it easier to manage their allocation and deallocation. Heap memory is used for dynamically allocated memory that may grow or shrink during program execution.

Why do we use const when passing arrays as arguments?

Passing arrays as const prevents the function from modifying the original array, ensuring the integrity of the data. If we don't want the function to modify the original array, we can pass it as a const. However, if modification is required, we should pass the array by reference using a pointer.

What is the difference between passing an array and passing a pointer to an array?

Passing an array directly to a function results in the entire array being passed by value, which can lead to performance issues for large arrays. Passing a pointer to an array allows us to modify the original array within the function, making it more efficient. When we pass a pointer, we should always specify the size of the array as a separate argument.