You are on page 1of 7

Function Pointers

As we know by definition that pointers point to an address in any memory location,


they can also point to at the beginning of executable code as functions in memory.
A pointer to function is declared with the *, the general statement of its declaration
is:
return_type (*function_name) (arguments)
You have to remember that the parentheses around (*function_name) are important
because without them, the compiler will think the function_name is returning a
pointer of return_type.
After defining the function pointer, we have to assign it to a function. For example,
the next program declares an ordinary function, defines a function pointer, assigns
the function pointer to the ordinary function and after that calls the function through
the pointer:

#include <stdio.h>
void Hi_function (int times); /* function */
int main()
{
void (*function_ptr)(int); /* function pointer Declaration */
function_ptr = Hi_function; /* pointer assignment */
function_ptr (3); /* function call */
return 0;
}
void Hi_function (int times)
{
int k;
for (k = 0; k < times; k++)
printf("Hi\n");
}
1. We define and declare a standard function which prints a Hi text k times
indicated by the parameter times when the function is called
2. We define a pointer function (with its special declaration) which takes an
integer parameter and doesn't return anything.
3. We initialize our pointer function with the Hi_function which means that the
pointer points to the Hi_function().
4. Rather than the standard function calling by taping the function name with
arguments, we call only the pointer function by passing the number 3 as
arguments, and that's it!

Keep in mind that the function name points to the beginning address of the
executable code like an array name which points to its first element. Therefore,
instructions like function_ptr = &Hi_function and (*funptr)(3) are correct.
NOTE: It is not important to insert the address operator & and the indirection
operator * during the function assignment and function call.

Pointers and Functions (Using pointer as function arguments)


The arguments to the functions can be passed in two ways-

1. Call by Value
In this parameter passing method, values of actual parameters are copied to
function’s formal parameters and the two types of parameters are stored in different
memory locations. So any changes made inside functions are not reflected in actual
parameters of the caller.
/* Program to understand call by value */
#include<stdio.h>
#include<conio.h>
int value (int, int);
int main()
{
int a=5, b=8;
printf("\n Before calling the function a=%d and b=%d",a,b);
value (a,b);
printf("\n After calling the function a=%d and b=%d",a,b);
getch();
}
int value (int a, int b)
{
a++;
b++;
printf("\n Inside function a=%d and b=%d",a,b);
}

2. Call by Reference
Both the actual and formal parameters refer to the same locations, so any changes
made inside the function are actually reflected in actual parameters of the caller.
/* Program to understand call by reference */
#include<stdio.h>
#include<conio.h>
int ref (int *a, int *b);
int main()
{
int a=5, b=8;
printf("\n Before calling the function a=%d and b=%d",a,b);
ref (&a,&b);
printf("\n After calling the function a=%d and b=%d",a,b);
getch();
}
int ref (int *a, int *b)
{
(*a)++;
(*b)++;
printf("\n Inside function a=%d and b=%d",*a,*b);}
// C program to illustrate // C program to illustrate
// Call by Value // Call by Reference
#include<stdio.h> #include<stdio.h>
// Function Prototype // Function Prototype
void swapx(int x, int y); void swapx(int*, int*);
// Main function // Main function
int main() int main()

{ {
int a = 10, b = 20; int a = 10, b = 20;
// Pass by Values // Pass reference
swapx(a, b); swapx(&a, &b);
printf("a=%d b=%d\n", a, b); printf("a=%d b=%d\n", a, b);
return 0; return 0;
} }
// Swap functions that swaps // Function to swap two variables
// two values // by references
void swapx(int x, int y) void swapx(int* x, int* y)
{ {
int t; int t;
t = x; t = *x;
x = y; *x = *y;
y = t; *y = t;
printf("x=%d y=%d\n", x, y); printf("x=%d y=%d\n", *x, *y);
} }

Output: Output:
x=20 y=10 x=20 y=10
a=10 b=20 a=20 b=10
Call by Value vs. Call by Reference

Parameters Call by value Call by reference


While calling a function, when While calling a function, in programming
you pass values by copying language instead of copying the values of
Definition
variables, it is known as "Call By variables, the address of the variables is
Values." used it is known as "Call By References.
In this method, a copy of the
Arguments In this method, a variable itself is passed.
variable is passed.
Changes made in a copy of
Change in the variable also affects the valu
Effect variable never modify the value
of the variable outside the function.
of variable outside the function.
Alteration of Does not allow you to make any Allows you to make changes in the values o
value changes in the actual variables. variables by using function calls.
Passing of Values of variables are passed Pointer variables are required to store the
variable using a straightforward method. address of variables.
Value
Original value not modified. The original value is modified.
modification
Actual and formal arguments will
Memory Actual and formal arguments will be created
be created in different memory
Location in the same memory location
location
Actual arguments remain safe as Actual arguments are not Safe. They can b
Safety they cannot be modified accidentally modified, so you need to handl
accidentally. arguments operations carefully.

Default in many programming


It is supported by most programming
Default languages like C++.PHP. Visual
languages like JAVA, but not as default.
Basic NET, and C#.
Pointer Operations and Declarations
We can perform various operations on pointer like operations on ordinary variables.
To illustrate the pointer operations, let us consider following declaration of ordinary
variables and pointer variables.
int a ,b;
float c;
int *p1, *p2;
float *f;

1. Address of an ordinary variable of a particular type can be assigned to a pointer


variable the same type.
For example:
p1 = &a; p2=&b; f=&c;

2. The content of one pointer can be assigned to another pointer variable provided
they both are of the same data type.
For example:
p1=p2; /* valid */
f=p1; /* invalid as two pointers are not of same type */

3. An integer can be added to or subtracted from a pointer. For examples:


 p1+2: It specifies an address which is two memory blocks for integer
(i.e. 4 memory bytes) beyond the address pointed by p1.

65516 65518 65520 65522 65524


p1 p1+1 p1+2 p1+3 p1+4

Here p1 is integer type pointer. If p1 points or stores address 65516, then p1+1 means
address pointed by p1+ size in bytes of data type of pointer i.e. 65516+2=65518.
Thus p1 represent address 65518 instead of 65517.

 f+1: It specifies an address which is one memory block for float data
(i.e. 4 memory bytes) beyond the address pointed by f.

65506 65510 65514 65518 65518
f f+1 f+2 f+3 f+4
Here f is float type pointer. If pointer f points address 65506, then f+1 points next
block’s address. As float variable takes 4 bytes in size, the block address is 65510.
Thus, f+1= 65506 + one block of memory for float= 65506+4=65510.

4. Two pointer variables can be compared provided the both pointers are defined
as same type.
For examples:
if( p1<p2)
{
/* is valid comparison when p1 and p2 are of same type */
}

5. There is no sense (i.e. no meaning) in assigning an integer to a pointer


variable. For examples:
p1=100; /* no sense */
p2= 65516; /* also invalid. Here 65516 may be address but it cannot be
assigned directly like integer value */

6. Two pointer variables cannot be multiplied by each others and added together.
For examples:
p1 + p2; /* invalid */
p1* p2; /* invalid */

7. A pointer variable cannot be multiplied by a constant. For example:


p1*2; /* invalid */

8. NULL value can be assigned to a pointer variable.


p1= NULL; /* Valid. Here null represents value 0 */

Compiled by
Khanal Nishan

You might also like