C Return an Array

What is an Array?

An array is a type of data structure that stores a fixed-size of a homogeneous collection of data. In short, we can say that array is a collection of variables of the same type.

For example, if we want to declare 'n' number of variables, n1, n2...n., if we create all these variables individually, then it becomes a very tedious task. In such a case, we create an array of variables having the same type. Each element of an array can be accessed using an index of the element.

Let's first see how to pass a single-dimensional array to a function.

snippet
#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 above program, we have first created the array arr[] and then we pass this array to the function getarray(). The getarray() function prints all the elements of the array arr[].

Output

Return an Array in C

Passing array to a function as a pointer

Now, we will see how to pass an array to a function as a pointer.

snippet
#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 above code, we have passed the array to the function as a pointer. The function printarray() prints the elements of an array.

Output

Return an Array in C
Note
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

snippet
#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 above program, getarray() function returns a variable 'arr'. It returns a local variable, but it is an illegal memory location to be returned, which is allocated within a function in the stack. Since the program control comes back to the main() function, and all the variables in a stack are freed. Therefore, we can say that this program is returning memory location, which is already de-allocated, so the output of the program is a segmentation fault.

Output

Return an Array in C

There are three right ways of returning an array to a function:

  • Using dynamically allocated array
  • Using static array
  • Using structure
Return an Array in C

Returning array by passing an array which is to be returned as a parameter to the function.

snippet
#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

Return an Array in C

Returning array using malloc() function.

snippet
#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

Return an Array in C

Using Static Variable

snippet
#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 above code, we have created the variable arr[] as static in getarray() function, which is available throughout the program. Therefore, the function getarray() returns the actual memory location of the variable 'arr'.

Output

Return an Array in C

Using Structure

snippet
#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

Return an Array in C
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +