Problem Solving and Programming with C GITAM
Pointers and functions
Pointers are often passed to a function as arguments.
➔ Allows data items within the calling program to be accessed by the function, altered, and then
returned to the calling program in altered form.
➔ Called call-by-reference (or by address or by location).
Normally, arguments are passed to a function by value.
➔ The data items are copied to the function.
➔ Changes are not reflected in the calling program.
In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a
simple example that shows declaration and function call using function pointer.
#include<stdio.h>
void main()
{
int a, b;
a = 5;
b = 20;
swap (&a, &b);
printf (“\n a=%d, b=%d”, a, b);
}
void swap (int *x, int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
1 Department of Computer Science and Engineering