For instance, when we need to define multiple variables like n1, n2...nn, individually creating each one can be quite laborious. To alleviate this, we can group these variables into an array with a common data type. Accessing each variable within the array is possible by referring to its respective index.
Let's begin by understanding the process of sending a one-dimensional array to a function.
#include <stdio.h>
void getarray(int arr[])
{
printf("Elements of array are : ");
for(int i=0;i<5;i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[5]={45,67,34,78,90};
getarray(arr);
return 0;
}
In the preceding code, an array named arr is initialized initially and subsequently passed as an argument to the getarray function. Within the getarray function, all the elements of the array arr are displayed.
Output
Elements of array are : [value]
Passing array to a function as a pointer
Now, we will learn about passing an array to a function as a pointer.
#include <stdio.h>
void printarray(char *arr)
{
printf("Elements of array are : ");
for(int i=0;i<5;i++)
{
printf("%c ", arr[i]);
}
}
int main()
{
char arr[5]={'A','B','C','D','E'};
printarray(arr);
return 0;
}
In the given code snippet, an array is passed to the function as a pointer. The function printarray displays the contents of the array.
Output
Elements of array are : [value]
Note: From the above examples, we observe that array is passed to a function as a reference which means that array also persist outside the function.
How to return an array from a function
Returning pointer pointing to the array
#include <stdio.h>
int *getarray()
{
int arr[5];
printf("Enter the elements in an array : ");
for(int i=0;i<5;i++)
{
scanf("%d", &arr[i]);
}
return arr;
}
int main()
{
int *n;
n=getarray();
printf("\nElements of array are :");
for(int i=0;i<5;i++)
{
printf("%d", n[i]);
}
return 0;
}
In the provided code snippet, the getarray function delivers the 'arr' variable. Although 'arr' is a local variable, returning it is improper as it is allocated within the function's stack frame. When the program control reverts to the main function, all stack variables are released. Consequently, this program attempts to return a memory location that has already been deallocated, leading to a segmentation fault as the program output.
Output
Elements of array are : [value]
There are three right ways of returning an array to a function:
- Using dynamically allocated array
- Using static array
- Using structure
Passing an array as a parameter to a function in order to return the same array.
#include <stdio.h>
int *getarray(int *a)
{
printf("Enter the elements in an array : ");
for(int i=0;i<5;i++)
{
scanf("%d", &a[i]);
}
return a;
}
int main()
{
int *n;
int a[5];
n=getarray(a);
printf("\nElements of array are :");
for(int i=0;i<5;i++)
{
printf("%d", n[i]);
}
return 0;
}
Output
Elements of array are : [value]
Returning 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("\nEnter 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]!='\0';i++)
{
printf("%d ", ptr[i]);
}
return 0;
}
Output
Elements of array are : [value]
Using Static Variable
#include <stdio.h>
int *getarray()
{
static int arr[7];
printf("Enter the elements in an array : ");
for(int i=0;i<7;i++)
{
scanf("%d",&arr[i]);
}
return arr;
}
int main()
{
int *ptr;
ptr=getarray();
printf("\nElements that you have entered are :");
for(int i=0;i<7;i++)
{
printf("%d ", ptr[i]);
}
}
In the preceding code snippet, the array variable arr is defined as static within the function getarray, ensuring its availability across the entire program. Consequently, the getarray function provides the precise memory address of the variable ' arr '.
Output
Elements of array are : [value]
Using Structure
#include <stdio.h>
#include<malloc.h>
struct array
{
int arr[8];
};
struct array getarray()
{
struct array y;
printf("Enter the elements in an array : ");
for(int i=0;i<8;i++)
{
scanf("%d",&y.arr[i]);
}
return y;
}
int main()
{
struct array x=getarray();
printf("Elements that you have entered are :");
for(int i=0;x.arr[i]!='\0';i++)
{
printf("%d ", x.arr[i]);
}
return 0;
}
Output