3 easy ways to return array from function in C – 4

Return array from function in C

Sometimes we want our function to return an array but in C programming it is not allowed to return the complete array.
There are 3 ways to solve this problem.
1. By declaring a local array variable as static.
2. Using dynamically allocated array
3. With help of structure

C program to return an array from function

By declaring a local array variable as static.

we can return a pointer to an array.
While returning a pointer we have to mention the array’s name without square brackets.
For this we have to create a function which returns a pointer.
Syntax for function which returns a pointer.
return_type * function_name(parameter_list)
{
body_of_function;
}

#include<stdio.h>

int * myfunction()
{
int array1[] = {1,2,3,4,5};
return array1;
}

int main()
{
int *ptr,i;
ptr = myfunction();
for(i=0;i<5;i++)
{
printf("nptr[%d] = %d",i,ptr[i]);
}
return 0;
}

Output:

raff.c: In function ‘myfunction’:
raff.c:6:12: warning: function returns address of local variable [-Wreturn-local-addr]
6 | return array1;
| ^~~~~~

We can see that above program will generate a warning i.e. “function returns address of local variable [-Wreturn-local-addr]

This is because we are returning the address of the local variable, this is not preferable because the local variable will get destroyed from a memory once the function call is over.
To solve this issue we need to declare our local array variable as a static.

Correct code to return array from function using static keyword is given below.

#include <stdio.h>

int * myfunction()
{
static int array1[] = {1,2,3,4,5};
return array1;
}

int main()
{
int *ptr,i;
ptr = myfunction();
for(i=0;i<5;i++)
{
printf("nptr[%d] = %d",i,ptr[i]);
}
return 0;
}

Output:

ptr[0] = 1
ptr[1] = 2
ptr[2] = 3
ptr[3] = 4
ptr[4] = 5

Using dynamically allocated array

In this we have to use a pointer in parameter of function which will point to the array which will be passed in function as argument.
We can access the original array with help of a pointer.

#include <stdio.h> 
int * fun(int *array1)
{
array1[0] = 1;
array1[1] = 2;
array1[2] = 3;
array1[3] = 4;

return array1;
}

int main()
{
int ary[4],i;
int * ptr = fun(ary);
for(i=0;i<4;i++)
{
printf("nptr[%d] = %d", i, ptr[i]);
}
return 0;
}

Output:

ptr[0] = 1
ptr[1] = 2
ptr[2] = 3
ptr[3] = 4

With help of structure

In this we have to create a structure so that we can create/wrap an array in it.
Once the structure is created we have to create a function which returns an instance of that function.
We can access the array from the instance of a structure.

#include <stdio.h> 

struct struct1
{
int array1[10];
};

struct struct1 myfunction()
{
struct struct1 a;

a.array1[0] = 1;
a.array1[1] = 2;
a.array1[2] = 3;
a.array1[3] = 4;
a.array1[4] = 5;

return a;
}

int main()
{
struct struct1 ary = myfunction();
int i;
for(i=0;i<5;i++)
{
printf("nary.array1[%d] = %d", i, ary.array1[i]);
}
return 0;
}

Output:

ary.array1[0] = 1
ary.array1[1] = 2
ary.array1[2] = 3
ary.array1[3] = 4
ary.array1[4] = 5

CHECKOUT OTHER RELATED TOPICS

CHECKOUT OTHER QUIZZES

PRACTICALS IN C PROGRAM

Programs to understand the basic data types and I/O.
Programs on Operators and Expressions.
Programs on decision statements.
Programs on looping.
Programs on arrays.
Programs on functions.
Programs on structures and unions.
Programs on pointers.
Programs on string manipulations.
Practice Programs
1 – C program for leap year, easy and simple code
2 – Best C program to find sum of all digits
3 – Best C program to check Palindrome Number
4 – Best C program to check prime number
5 – Best C program to print Fibonacci series
6 – Best C program to reverse a number
7 – Best C program to reverse an array
8 – Best C program to find average of array
9 – Best C program to find frequency of array element
10 – Best C program to sort array in ascending order
11 – Best C program to find factorial using recursion
12 – Best C program to find sum of all numbers in array
13 – Best C program to find largest element in array
14 – Best C program to swap two numbers without third variable
15 – C Program to check if a number is even or odd
16 – C program to find Simple interest
17 – Best C program to add two matrices
18 – Best C program to count vowels and consonants in a string
19 – Best C program to find LCM
20 – Best C program to find HCF
21 – Best C program to swap two numbers using pointers
22 – Best C program to add two numbers using pointers
23 – Best C program to print Multiplication Table
24 – Best C program to check Armstrong number
25 – Best C program to print half pyramid of star(*)
26 – Best C program to print half pyramid of numbers
27 – Best C program to print reverse half pyramid of star
28 – Best C program to print reverse half pyramid of number
More coming soon…
We are aiming to explain all concepts of C in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment