Table of Contents
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
Quizzes on Operator |
Easy Quiz on Operators in C part 1 |
Easy quiz on operators in C part 2 |
Easy quiz on operators in C part 3 |
Quizzes on Loops |
Easy quiz on loops in C part 1 |
Easy quiz on loops in C part 2 |
Easy quiz on loops in C part 3 |
Quizzes on Decision Making Statement |
Easy Quiz On Decision Making Statements Part 1 |
Easy Quiz On Decision Making Statements Part 2 |
Quizzes on String |
Easy quiz on String in C programming part 1 |
Quizzes on Array |
Easy quiz on Array in C programming – part 1 |
5 – Best Java program to convert decimal to binary |
New Quizzes Coming soon.. |
PRACTICALS IN C PROGRAM
