Passing Arrays and Strings to function
Passing Arrays and Strings to function
THREE WAYS
Sized Array
UNSIZED ARRAY
int main()
myFunction(param)
POINTER
EXAMPLE 1-finding average
#include<stdio.h>
float average(int,int[]);
int main()
int n,i,marks[100];
float result;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&marks[i]);
result=average(n,marks);
printf("%f",result);
int total=0;
float avg;
int i;
for(i=0;i<n;i++)
total=total+marks[i];
avg=total/n;
return avg;
Count the unique elements in an array, given that the element value will lies between 0 and 1001.
Count the number of words in a given sentence.
Passing two-dimensional arrays to functions
1. The actual argument in the function call should only be the name of the array without any
subscript
2. The corresponding formal parameter in the function definition must be of array type or pointer
type (i.e. pointer to the first element of the array).
• If a formal parameter is of pointer type, it must be a pointer to an element of the two dimensional
array
3. The corresponding parameter type in the function declaration should be a matching array type or
pointer type.
#include<stdio.h>
int main()
{
int m,n;
int i,j,a[5][5],b[5][5],c[5][5];
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
return 0;
for(i=0;i<m;i++) {
for(j=0;j<n;j++) {
c[i][j] = a[i][j]+b[i][j];
}}
for(i=0;i<m;i++) {
for(j=0;j<n;j++) {
printf("%d ",c[i][j]);
} printf("\n");