Menu
×
   ❮     
HOME Computer Fundamentals C Language C++ MS-OFFICE TALLY 11/12 SCIENCE 11/12 COMM BCA BCCA BBA MCA
     ❯   

Computer

▸ Computer Fundamentals▸ What is Computer▸ History of Computer▸ Types of Computer▸ History of C Language▸ History of C Language

Computer Components

▸ Computer components▸ Input Devices▸ Output Devices▸ CPU▸ Hardware

Computer Memory

▸ Computer Memory▸ Register Memory

Computer Virus

▸ Computer virus▸ Computer virus 2

Computer Network

▸ Computer Network details

Number Systems

▸ Number Systems

C Control Statements

▸ C if else▸ C Switch Statement▸ if-else vs switch▸ C Loops▸ C Do- While Loop▸ C While Loop▸ C For Loop▸ C For Loop▸ Nested Loops in C▸ Infinite Loop in C▸ C break▸ C continue▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ Type Casting

C Functions

▸ What is Function▸ Recursion in C▸ Call: Value & Reference▸ Call: Value & Reference▸ Storage Classes▸ Storage Classes

C Array

▸ 1 D - Array▸ Return an Array in C▸ Array To Function ▸ 2 - D Array▸ Return an Array in C

C Pointers

▸ C Pointers▸ C Double Pointer (Pointer to Pointer)▸ C Double Pointer (Pointer to Pointer)

C Tutorial

▸ Data Types in C▸ Keywords in C▸ C Identifiers▸ Escape Sequence in C▸ ASCII value in C▸ Constants in C▸ Static in C▸ Programming Errors in C▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Conditional Operator in C▸ Bitwise Operator in C▸ Bitwise Operator in C▸ 2s complement in C▸ What is C Language▸ History of C Language▸ Features of C Language▸ How to install C▸ First C Program▸ Compilation process in c▸ Printf Scanft▸ Variables in C ▸ C Operators▸ Comments in C▸ C Format Specifier▸ Literals in C▸ Tokens in C▸ C Boolean▸ Programming Errors in C

What is an Array? ❮ Edit Details

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.

Passing array to a function

  1. #include <stdio.h>  
  2. void getarray(int arr[])  
  3. {  
  4.     printf("Elements of array are : ");  
  5.     for(int i=0;i<5;i++)  
  6.     {  
  7.         printf("%d ", arr[i]);  
  8.     }  
  9. }  
  10. int main()  
  11. {  
  12.    int arr[5]={45,67,34,78,90};  
  13.    getarray(arr);  
  14.    return 0;  
  15. }  

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.

  1. #include <stdio.h>  
  2. void printarray(char *arr)  
  3. {  
  4.     printf("Elements of array are : ");  
  5.     for(int i=0;i<5;i++)  
  6.     {  
  7.         printf("%c ", arr[i]);  
  8.     }  
  9. }  
  10. int main()  
  11. {  
  12.   char arr[5]={'A','B','C','D','E'};  
  13.   printarray(arr);  
  14.   return 0;  
  15. }  

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: 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

  1. #include <stdio.h>  
  2. int *getarray()  
  3. {  
  4.     int arr[5];  
  5.     printf("Enter the elements in an array : ");  
  6.     for(int i=0;i<5;i++)  
  7.     {  
  8.         scanf("%d", &arr[i]);  
  9.     }  
  10.     return arr;  
  11. }  
  12. int main()  
  13. {  
  14.   int *n;  
  15.   n=getarray();  
  16.   printf("\nElements of array are :");  
  17.   for(int i=0;i<5;i++)  
  18.     {  
  19.         printf("%d", n[i]);  
  20.     }  
  21.     return 0;  
  22. }  

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.

  1. #include <stdio.h>  
  2. int *getarray(int *a)  
  3. {  
  4.     
  5.     printf("Enter the elements in an array : ");  
  6.     for(int i=0;i<5;i++)  
  7.     {  
  8.         scanf("%d", &a[i]);  
  9.     }  
  10.     return a;  
  11. }  
  12. int main()  
  13. {  
  14.   int *n;  
  15.   int a[5];  
  16.   n=getarray(a);  
  17.   printf("\nElements of array are :");  
  18.   for(int i=0;i<5;i++)  
  19.     {  
  20.         printf("%d", n[i]);  
  21.     }  
  22.     return 0;  
  23. }  

Output

Return an Array in C

Returning array using malloc() function.

  1. #include <stdio.h>  
  2. #include<malloc.h>  
  3. int *getarray()  
  4. {  
  5.     int size;  
  6.     printf("Enter the size of the array : ");  
  7.     scanf("%d",&size);  
  8.     int *p= malloc(sizeof(size));  
  9.     printf("\nEnter the elements in an array");  
  10.     for(int i=0;i<size;i++)  
  11.     {  
  12.         scanf("%d",&p[i]);  
  13.     }  
  14.     return p;  
  15. }  
  16. int main()  
  17. {  
  18.    int *ptr;  
  19.    ptr=getarray();  
  20.    int length=sizeof(*ptr);  
  21.    printf("Elements that you have entered are : ");  
  22.    for(int i=0;ptr[i]!='\0';i++)  
  23.     {  
  24.       printf("%d ", ptr[i]);  
  25.     }  
  26.   return 0;  
  27. }  

Output

Return an Array in C

Using Static Variable

  1. #include <stdio.h>  
  2. int *getarray()  
  3. {  
  4.   static int arr[7];  
  5.   printf("Enter the elements in an array : ");  
  6.   for(int i=0;i<7;i++)  
  7.   {  
  8.       scanf("%d",&arr[i]);  
  9.   }  
  10.   return arr;  
  11.       
  12. }  
  13. int main()  
  14. {  
  15.   int *ptr;  
  16.   ptr=getarray();  
  17.   printf("\nElements that you have entered are :");  
  18.   for(int i=0;i<7;i++)  
  19.   {  
  20.       printf("%d ", ptr[i]);  
  21.   }  
  22. }  

In the above code, we have created the variable arr[] as static in getarray() function, which is available throughout the program. Th

erefore, the function getarray() returns the actual memory location of the variable 'arr'.

Output

Return an Array in C

Using Structure

The structure is a user-defined data type that can contain a collection of items of different types. Now, we will create a program that returns an array by using structure.

 

  1. #include <stdio.h>  
  2. #include<malloc.h>  
  3. struct array  
  4. {  
  5.     int arr[8];  
  6. };  
  7. struct array getarray()  
  8. {  
  9.     struct array y;  
  10.     printf("Enter the elements in an array : ");  
  11.     for(int i=0;i<8;i++)  
  12.     {  
  13.         scanf("%d",&y.arr[i]);  
  14.     }  
  15.     return y;  
  16. }  
  17. int main()  
  18. {  
  19.   struct array x=getarray();  
  20.   printf("Elements that you have entered are :");  
  21.   for(int i=0;x.arr[i]!='\0';i++)  
  22.   {  
  23.       printf("%d ", x.arr[i]);  
  24.   }  
  25.     return 0;  
  26. }  

Output

Return an Array in C