Introduction
Fundamentals
C Flow Control
C Functions
Returning Arrays from Functions in C
In this tutorial, we will learn about how to return arrays from functions in C and how to use them effectively in your programs. This comprehensive guide includes examples and best practices for returning arrays from functions in C.
In C we can pass single dimensional arrays in two ways. We can either pass it directly to a function or we can also pass it as a pointer to array. In addition to being passed an array, a function in C can return an array.
It is seen as an important task to Pass arrays to a function in C as to be passed as the actual parameters from the main function some amount of numbers is required.
How to return an array from a function ?
We can return a pointer to the base address (address of first element of array) of array from a function like any basic data type. However, C programming language does not allow to return whole array from a function.
If we want to return a local array then we should declare it as a static variable so that it retains it's value after function call.
Declaration of a Function Returning Integer Array:
int* getScoreList();
C Program to Return an Array from a Function:
#include<stdio.h>
#include <stdio.h>
#include <conio.h>
/* This function returns an array of N even numbers */
int* getEvenNumbers(int N){
/* Declaration of a static local integer array */
static int evenNumberArray[100];
int i, even = 2;
for(i=0; i<N; i++){
evenNumberArray[i] = even;
even += 2;
}
/* Returning base address of evenNumberArray array*/
return evenNumberArray;
}
int main(){
int *array, counter;
array = getEvenNumbers(10);
printf("Even Numbers");
for(counter=0; counter<5; counter++){
printf("%d", array[counter]);
}
getch();
return 0;
}
Output :
Even Numbers 2 4 6 8 10
Return array using malloc() function.
Here, we will return an array using malloc() Function:
#include <stdio.h>
#include<malloc.h>
int *getarray()
{
int size;
printf("Enter the size of the array : ");
scanf("%d",&size);
int *p= malloc(sizeof(size));
printf("Enter the elements in an array :");
for(int i=0;i<size;i++)
{
scanf("%d",&p[i]);
}
return p;
}
int main()
{
int *ptr;
ptr=getarray();
int length=sizeof(*ptr);
printf("Elements that you have entered are : ");
for(int i=0;ptr[i]!='';i++)
{
printf("%d ", ptr[i]);
}
return 0;
}
Output :
Enter the size of the array : 4 Enter the elements in an array : 2 3 4 5 Elements that you have entered are : 2 3 4 5
Pass the returned array as Parameter:
Arrays in C are passed by reference, hence any changes made to array passed as argument persists after the function. So you can accept the output array you need to return, as a parameter to the function.
#include <stdio.h>
#define MAX_SIZE 10
/* Function delcaration to initialize array and return */
void getArray(int arr[], int size);
int main()
{
int arr[MAX_SIZE];
int i;
// Call function to initialize array.
getArray(arr, MAX_SIZE);
printf("Array outside function:");
for (i = 0; i < MAX_SIZE; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
/**
* Function to initialize array.
*
* @arr Integer array to initialize and return.
* @size Size of the array.
*/
void getArray(int arr[], int size)
{
int i;
printf("Enter elements in array: ");
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
printf("Array inside function:");
for (i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
}
Output :
Enter elements in array: 1 2 3 4 5 6 7 8 9 10 Array inside function: 1 2 3 4 5 6 7 8 9 10 Array outside function: 1 2 3 4 5 6 7 8 9 10