Return an Array from a Function in C
In C, a function cannot return an entire array directly, but it can return a pointer to an array. This is achieved by using static arrays, dynamic memory allocation, or passing an array to be modified by reference.
In this tutorial, we will explore different methods to return an array from a function in C.
Examples to Return an Array from a Function
1. Returning a Static Array
In this example, we will define a function that returns a pointer to a static array. The static storage class ensures that the array persists after the function ends.
main.c
#include <stdio.h>
int* getArray() {
static int arr[] = {1, 2, 3, 4, 5}; // Static array
return arr; // Returning pointer to the array
}
int main() {
int* ptr = getArray();
// Printing the returned array
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]);
}
return 0;
}
Explanation:
- The function
getArray()
declares a static integer arrayarr[]
with five elements. - Since the array is static, it retains its value after the function returns.
- The function returns a pointer to the first element of the array.
- In
main()
, we store the returned pointer inptr
and use afor
loop to print the elements.
Output:
1 2 3 4 5
2. Returning an Array Using Dynamic Memory Allocation
In this example, we will use malloc()
to allocate memory dynamically and return an array from the function.
main.c
#include <stdio.h>
#include <stdlib.h>
int* createArray(int size) {
int* arr = (int*)malloc(size * sizeof(int)); // Allocate memory dynamically
for (int i = 0; i < size; i++) {
arr[i] = i * 10; // Assign values
}
return arr;
}
int main() {
int size = 5;
int* ptr = createArray(size);
// Printing the returned array
for (int i = 0; i < size; i++) {
printf("%d ", ptr[i]);
}
free(ptr); // Free allocated memory
return 0;
}
Explanation:
- The function
createArray()
usesmalloc()
to allocate memory dynamically for an integer array. - The array elements are initialized with values using a loop.
- The function returns the pointer to the allocated memory.
- In
main()
, we callcreateArray()
and store the returned pointer inptr
. - The values are printed using a
for
loop. - Finally, we free the allocated memory using
free(ptr)
to prevent memory leaks.
Output:
0 10 20 30 40
3. Returning an Array by Modifying a Passed Array
In this example, we pass an array to a function by reference and modify its values inside the function.
main.c
#include <stdio.h>
void modifyArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
arr[i] *= 2; // Multiply each element by 2
}
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int size = sizeof(numbers) / sizeof(numbers[0]);
modifyArray(numbers, size);
// Printing modified array
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Explanation:
- The function
modifyArray()
accepts an array and its size as arguments. - Since arrays are passed by reference in C, changes inside the function affect the original array.
- Each element of the array is multiplied by 2 inside the loop.
- In
main()
, the function is called withnumbers[]
as the argument. - After the function call, the modified array is printed.
Output:
2 4 6 8 10
Conclusion
In this tutorial, we explored three different ways to return an array from a function in C:
- Using a static array: The function returns a pointer to a static array.
- Using dynamic memory allocation:
malloc()
is used to allocate memory and return an array. - Modifying a passed array: The function modifies an array passed as an argument.