Monday 16 December 2013

[C Programming] Function Pointers basics in C

Function Pointers

Definition

Function pointers are pointers, which points to the address of a function. Function pointers are variables only, so it will be declared and defined like other variable.

Syntax

<data_type> (*function_pointer_name) (variable data_type list) = NULL; /* defining function pointer and initializes to NULL */

The signature of this function pointer should match with the signature of function which address is stored in this pointer.

Sample code

float doMath (float a, float b) {
 /*
    desired code
 */
return result;
}

int main() {
float (*math_pointer) (float, float) = NULL;

math_pointer = &doMath; /* pointer to function doMath() */

output = math_pointer (10.5, 11.1);
/*
   desired code
*/
return 0;
}


Usage

  1. To replace switch/if conditional statements
  2. To implement late-binding (runtime binding) (C++ scenario)
  3. To implement callbacks

Example for Usage-1

#include<stdio.h>

float addition (float a, float b) {
 return a + b;
}

float subtraction (float a, float b) {
 return a - b;
}

float multiplication (float a, float b) {
 return a * b;
}

void select (float a, float b, char opCode) {
float result;
switch(opCode)
{
 case '+': result = addition(a, b); break;
 case '-': result = subtraction(a, b); break;
 case '*': result = multiplication (a, b); break;
}
printf("Result: %f\n", result);
}

/* Replace the above switch statement with function pointer */
void select_function_pointer (float a, float b, float (*math_pointer)(float, float)) {
float result = math_pointer(a, b); /* Calling function using function pointer */
printf("Result: %f\n", result);
}

void main()
{
  float a, b;
   
  select(10, 15, '+');
  select_function_pointer (10, 15, &addition); /* Passing function as an address argument */
}


Array of Function Pointers

Since the Function pointer is a variable, we can define array of function pointer using below syntax.


Syntax-I
tyepdef <data_type> function_pointer_name (argument data_type list);
function_pointer_name array_fun_pointer [10] = {NULL};

Syntax-II
<data_type> (*array_fun_pointer[10])(argument data_type list) = {NULL};

array_fun_pointer [0] = &function1;
.
.
Calling function using function pointer
(*array_fun_pointer[0]) (arguments); or
array_fun_pointer[0](arguments);


Implementation of Callbacks using Function Pointers

It is a bit more complex syntax. Try to understand the callback mechanism in C by using below sample code:

void insert_array (int *array, size_t size, int (*getNextValue) (void))
{
     for (int i = 0; i < size; i++)
        array[i] = getNextValue();
}

int getNextRandomValue()
{
   return rand();
}

int main()
{
   int a[10];
   insert_array(a, 10, &getNextRandomValue);
}

Here, the function insert_array() takes the 3rd argument as the function address using function pointer getNextValue. And, the callback function is getNextRandomValue(), returns random value. The function insert_array() calls this callback function using function pointer getNextValue for inserting the values into the array.

The similar explanation can be found here (from Stackoverflow)

No comments:

Post a Comment

You might also like

Related Posts Plugin for WordPress, Blogger...