Table of Contents
Passing Array to Function in C
We can pass arrays to the function.
We have to declare array variables in parameters of function which can hold the value of array from argument.
If we declare an array variable (not a pointer) in parameter then the operation performed in the function will not affect the original array.
There are following methods to do so.
Passing Array as Function argument in C
Unsized array as a parameter variable.
return_type function_name(data_type parameter[])
{
body _of_function;
}
Sized array as a parameter variable.
return_type function_name(data_type parameter[size])
{
body _of_function;
}
Pointer variable as a parameter variable.
return_type function_name(data_type *parameter)
{
body _of_function;
}
Passing Array to Function example
#include<stdio.h>
int sum(int a[],int len)
{
int rsl=0,i;
for(i=0;i<len;i++)
{
rsl+=a[i];
}
return rsl;
}
int main()
{
int total,nums[] = {45,98,12,65,95,59,29,15,36};
int l = sizeof(nums)/sizeof(int);
total = sum(nums,l);
printf("Sum of all numbers in array is %d",total);
return 0;
}
Output :
Sum of all numbers in array is 454
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
We are aiming to explain all concepts of C in easiest terms as possible.

ITVoyagers
Author