Programming in C: Unit III (b): Pointers

Passing Arguments to Function Using Pointers

with Example C Programs

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: Unit III (b): Pointers



Under Subject


Programming in C

CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation



Related Subjects


Professional English II

HS3251 2nd Semester 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation


Statistics and Numerical Methods

MA3251 2nd Semester 2021 Regulation M2 Engineering Mathematics 2 | 2nd Semester Common to all Dept 2021 Regulation


Engineering Graphics

GE3251 eg 2nd semester | 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation


Physics for Electrical Engineering

PH3202 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Basic Civil and Mechanical Engineering

BE3255 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Electric Circuit Analysis

EE3251 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Physics for Electronics Engineering

PH3254 - Physics II - 2nd Semester - ECE Department - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Electrical and Instrumentation Engineering

BE3254 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Circuit Analysis

EC3251 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Materials Science

PH3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation


Basic Electrical and Electronics Engineering

BE3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation


Physics for Civil Engineering

PH3201 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation


Basic Electrical, Electronics and Instrumentation Engineering

BE3252 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation


Physics for Information Science

PH3256 2nd Semester CSE Dept | 2021 Regulation | 2nd Semester CSE Dept 2021 Regulation


Basic Electrical and Electronics Engineering

BE3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation


Programming in C

CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation