Pages

This blog is under construction

Thursday, January 17, 2019

How to pass an array to a function in C Programming?

Whenever we need to pass a list of elements as an argument to any function in C language, it is preferred to do so using an array. There are two ways to pass:
1: pass element one by one to the function 
2: pass the entire array to the function.

Declaring Function with an array as a parameter
There are also two possible ways to do so, 
1: using call by value 
2: using call by reference.
We can either have an array as a parameter. 
 int sum(int arr [ ])
we can have a pointer in the parameter list, to hold the base address of our array.
int sum(int *ptr)

Example:
Write a c program to find the sum of elements using above both syntaxes.
a) Pass element one by one

#include <stdio.h>
 
int main()

  { int arr[5],i,t;
      printf("\nEnter elements of an array:\n");
    for(i=0;i<5;i++)
      { 
         scanf("%d",&arr[i]);
      }
    
    for(i=0;i<5;i++)
    {
     t=sum(arr[i]);
    }
    printf("\nSum of elements=%d",t);
  }
   
   int sum(int ele)
     { static int total=0;
       total=total+ele;
       return total;
     }  
   
        
b) Pass entire array to the function 

1) int sum(int arr [ ]);
#include <stdio.h>

int main()

  { int arr[5],i,total;
    printf("\nEnter elements of an array:\n");
    for(i=0;i<5;i++)
      { 
         scanf("%d",&arr[i]);
      }
     
     total=sum(arr);
    printf("\nSum of elements=%d",total);
  }
   
   int sum(int my_arr[])
    { int t=0,i;
    for(i=0;i<5;i++)
       {
          t=t+my_arr[i];
        }
       return t;
     }
     
Output:

2) int sum(int *ptr)

    #include <stdio.h>

int main()

  { int arr[5],i,total;
    printf("\nEnter elements of an array:\n");
    for(i=0;i<5;i++)
      { 
         scanf("%d",&arr[i]);
      }
     
     total=sum(arr);
    printf("\nSum of elements=%d",total);
  }
    
   int sum(int *ptr)
    { int t=0,i;
    for(i=0;i<5;i++)
       {
          t=t+*(ptr+i);
        }
       return t;
     }
     
Enter elements of an array:                                                                                                      
3                                                                                                                                
3                                                                                                                                
3                                                                                                                                
3                                                                                                                                
3                                                                                                                                
                                                                                                                                 
Sum of elements=15    
    
    
    
    
    
    


 



No comments:

Post a Comment