C Programming and Data Structures: Unit II: C Programming – Advanced Features

Arrays and Functions

Syntax, Example C programs

Thus instead of actual value, the address is passed as a parameter to the function hence it is called parameter passing by address.

Arrays and Functions

When an array is passed as an argument to the function then we are actually passing the base address of an array to the function. Using this base address we can access the remaining elements of the array in the function. Thus instead of actual value, the address is passed as a parameter to the function hence it is called parameter passing by address.

 We will understand this concept with the help of following example -

#include<stdio.h>

#include<conio.h>

void main()

{

void fun(int a[5]);/*function declaration*/

int a[]={10,20,30,40,50};

 clrscr();

fun(a);/*call to the function*/   Passing the base address of an array

getch();

}

void fun(int a[6])

int i;

}

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

printf("%d",*(a+i));

/* or printf("%d",a[i]);*/

}

Output

10 20 30 40 50

In above program array name is passed as an argument to the function then automatically the base address is passed to the function. This should be done when we give call to the function. While declaring such a function the array name along with its data type and square brackets [ ] should be specified. Even though we do not specify the actual size, it is allowed in C.

For example :

While declaring the function with an argument as array -

void fun(int);/*is invalid*/

void fun(a);/*is invalid*/

void fun(a[ ]);/*is invalid*/

void fun(int a);/*is invalid*/

But,

void fun(int a[6]); /*is valid*/

void fun(int a[ ]); /*is valid*/

void fun(int [6]); /*is valid*/

void fun(int []);/*is valid*/

 Similarly in function definition -

void fun(int [6]) /*is invalid*/

void fun(int a) /*is invalid*/

void fun(int[])/*is invalid*/

void fun(a[6]) /*is invalid*/

void fun(int) /*is invalid*/

But

void fun(int a[6]); /*is valid*/

void fun(int a[]); /*is valid*/

Program: Write a C program to calculate the sum of all the integers in an array using pointer.

#include<stdio.h>

#include<conio.h>

void main()

{

int fun(int []);/*function declaration*/

int a[]={10,20,30,40,50};

clrscr();

printf("\nSum = %d",fun(a));/*call to the function*/

getch();

}

int fun(int a[])

{

int i,sum=0;

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

sum=sum+ *(a+i);

Using pointer the next location in array is = (a + i) and the value at that location is obtained by * (a + i)

return sum;

}

Output

Sum = 150

 

C Programming and Data Structures: Unit II: C Programming – Advanced Features : Tag: : Syntax, Example C programs - Arrays and Functions


C Programming and Data Structures: Unit II: C Programming – Advanced Features



Under Subject


C Programming and Data Structures

CS3353 3rd Semester EEE, ECE Dept | 2021 Regulation | 3rd Semester EEE Dept 2021 Regulation



Related Subjects


Probability and complex function

MA3303 3rd Semester EEE Dept | 2021 Regulation | 3rd Semester EEE Dept 2021 Regulation


Electromagnetic Theory

EE3301 3rd Semester EEE Dept | 2021 Regulation | 3rd Semester EEE Dept 2021 Regulation


Digital Logic Circuits

EE3302 3rd Semester EEE Dept | 2021 Regulation | 3rd Semester EEE Dept 2021 Regulation


Electron Devices and Circuits

EC3301 3rd Semester EEE Dept | 2021 Regulation | 3rd Semester EEE Dept 2021 Regulation


Electrical Machines I

EE3303 EM 1 3rd Semester EEE Dept | 2021 Regulation | 3rd Semester EEE Dept 2021 Regulation


C Programming and Data Structures

CS3353 3rd Semester EEE, ECE Dept | 2021 Regulation | 3rd Semester EEE Dept 2021 Regulation