0% found this document useful (0 votes)
6 views

Passing Arrays and Strings to function

The document discusses methods for passing arrays and strings to functions in C programming, including sized and unsized arrays as well as pointers. It provides examples for calculating the average of an array and performing matrix addition using functions. Additionally, it outlines the syntactic rules for passing two-dimensional arrays to functions.

Uploaded by

Alessandro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Passing Arrays and Strings to function

The document discusses methods for passing arrays and strings to functions in C programming, including sized and unsized arrays as well as pointers. It provides examples for calculating the average of an array and performing matrix addition using functions. Additionally, it outlines the syntactic rules for passing two-dimensional arrays to functions.

Uploaded by

Alessandro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Week 13 – Passing Arrays and Strings to Functions

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);

float average(int n,int marks[])

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

The syntactic rules to pass two-dimensional arrays to a function are as follows,

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 array type, it is mandatory to specify the column specifiers.

• 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.

/* matrix addition using functions*/

#include<stdio.h>

int main()
{

int m,n;

int i,j,a[5][5],b[5][5],c[5][5];

printf("Enter the dimensions of the matrices: ");

scanf("%d%d",&m,&n);

void mul(int[5][5],int[5][5],int,int); /*function declaration*/

printf("Enter the matrix A \n");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

scanf("%d",&a[i][j]);

printf("enter the matrix B \n");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

scanf("%d",&b[i][j]);

mul(a,b,m,n); /* function call */

return 0;

void mul(int a[5][5],int b[5][5],int m,int n) /*function definition*/

int i,j; int c[5][5];

for(i=0;i<m;i++) {

for(j=0;j<n;j++) {
c[i][j] = a[i][j]+b[i][j];

}}

printf( "\n sum of two matrices are \n");

for(i=0;i<m;i++) {

for(j=0;j<n;j++) {

printf("%d ",c[i][j]);

} printf("\n");

Passing String to Function.


Ans: A
Ans: C
Ans: C
Ans: A
Ans: C

You might also like