Home C-tutorials C Pointer to a function



C Pointer to a function
Posted on: February 5, 2009 at 12:00 AM
C provides a special feature of pointer to a function. As you know that every function defined in C language have a base address attached to it.

C Pointer to a function

     

In this section, you will learn how to use 'Pointer'  function in C.

C provides a special feature of  pointer to a function. As you know  that every function defined in C language have a base address attached to it. This base address acts as an entering point into that function. This address can be stored in a pointer known as function pointer. The pointer to a function is the declaration of pointer that holds the base address of the function. The declaration of a pointer to a function is:

Syntax :
return_type (* pointer_name) ( variable1_type variable1_name  , variable2_type variable2_name , variable3_type variable3_name .................);  

You can see in the given example, we have create a function mul to find the product of three numbers. Then we have declared the function pointer for storing the base address of function mul in the following way:

int (*function_pointer) (int,int,int); 

Here is the code:

POINTERF.C

#include <stdio.h>
#include <conio.h>
int mul(int a, int b, int c) {
  return a*b*c;
}
void main() {
  int (*function_pointer)(int, int, int);
  function_pointer = mul;
  printf("The product of three numbers is:%d",
    function_pointer
(234));
  getch();
}

Output will be displayed as:

Download the code

Related Tags for C Pointer to a function:
cidefunctionlanguagefuniopointerviintidaddressdefinepoiuncaddtopointattachfeaturebasecilaneitdespeinnoasntddadesspecialspecfuncaseageprodefinedsspessatkhaeattssthavhatfefinprono


More Tutorials from this section

Ask Questions?    Discuss: C Pointer to a function   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.