We have already seen call-by-value method of passing parametres to a function. Using call-by-value method, it is impossible to modify the actual parameters when you pass them to a function.
PASSING
ARGUMENTS TO FUNCTION USING POINTERS
We
have already seen call-by-value method of passing parametres to a function.
Using call-by-value method, it is impossible to modify the actual parameters
when you pass them to a function. Furthermore, the incoming arguments to a
function are treated as local variables in the function and those local
variables get a copy of the values passed from their calling function.
Pointers
provide a mechanism to modify data declared in one function using code written
in another function. In other words: If data is declared in func1() and we want to write code in func2 () that modifies the data in func1 (), then we must pass the
addresses of the variables to func2().
Programming Tip:
While
using pointers to pass arguments to a function, the calling function must pass
addresses of the variables as arguments and the called function must
dereference the arguments to use them in the function body for processing.
The
calling function sends the addresses of the variables and the called function
must declare those incoming arguments as pointers. In order to modify the
variables sent by the calling function, the called function must dereference
the pointers that were passed to it. Thus, passing pointers to a function
avoids the overhead of copying data from one function to another. Hence, to use
pointers for passing arguments to a function, the programmer must do the
following:
•
Declare the function parametres as pointers
•
Use the dereferenced pointers in the function body
•
Pass the addresses as the actual argument when the function is called.
Note
It
is an error to return a pointer to a local variable in the called function,
because when the function terminates, its memory may be allocated to a
different program.
Let
us write some programs that pass pointer variables as parameters to functions.
14. Write a program
to add two integers using functions
#include <stdio.h>
#include <conio.h>
void sum (int *a, int *b, int *t);
int main()
{
int num1, num2, total;
printf("\n Enter the first
number: ");
scanf("%d", &num1);
printf("\n Enter the second
number: ");
scanf("%d", &num2);
sum (&num1, &num2,
&total);
printf("\n Total = %d",
total);
getch();
return 0;
}
void sum (int *a, int *b, int *t)
{
*t = *a + *b;
}
Output
Enter the first number: 2
Enter the second number: 3
Total = 5
15. Write a program,
using functions, to find the biggest of three integers.
#include <stdio.h>
int greater (int *a, int *b, int
*c, int *large);
int main()
{
int num1, num2, num3, large;
printf("\n Enter the first
number: ");
scanf("%d", &num1);
printf("\n Enter the second
number: ");
scanf("%d", &num2);
printf("\n Enter the third
number: ");
scanf("%d", &num3);
greater (&num1, &num2,
&num3, &large);
return 0;
}
Programming Tip: A
compiler error will be generated if you use pointer arithmetic with multiply,
divide or modulo operators.
int greater (int *a, int *b, int
*c, int *large)
{
if (*a > *b && *a >
*c) * large = *a;
if (*b > *a && *b >
*c) * large = *b;
else
* large = *C;
printf("\n Largest number =
%d", *large);
}
Output
Enter the first number: 1
Enter the second number: 7
Enter the third number: 9
largest number = 9
16. Write a program
to calculate area of a triangle.
#include <stdio.h>
void read (float *b, float *h);
void calculate_area (float *b,
float *h, float *a);
int main()
{
float base, height, area;
read (&base, &height);
calculate_area (&base, &height,
&area);
printf("\n Area of the
triangle with base %.1f and height%.1f = %.2f", base, height, area);
return 0;
}
void read (float *b, float *h)
{
printf("\n Enter the base of
the triangle: ");
scanf("%f", b);
printf("\n Enter the height of
the triangle: ");
scanf("%f", h);
void calculate area (float *b,
float *h, float *a)
{
*a = 0.5 * (*b) * (*h);
}
Output
Enter the base of the triangle : 10
Enter the height of the triangle :
5
Area of the triangle with base 10.0
and height 5.0 = 25.00
We
know that functions usually return only one value, but pointers can be used to
return more than one value to the calling function. This is done by allowing
the arguments to be passed by addresses which enables the function to alter the
values pointed to and thus helps to return more than one value.
Programming in C: Unit III (b): Pointers : Tag: : with Example C Programs - Passing Arguments to Function Using Pointers
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation