Home C-tutorials C Pointer to a function
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

C Pointer to a function

Advertisement
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

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 5, 2009

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
rudraswamy
March 31, 2011
pointers in c

actually i know basic of c....but i dnt know how to study pointers in c...give me some sugesstion for me
Thakur
June 9, 2011
Pointer to function

Thanks providing this article..........
Inderpreet Singh
October 6, 2011
Thanks

Thanks for providing information. You have written the complex topic in a very simple and easy form.It was much benificial for me.
Zakir Shaikh
October 20, 2011
Computer Science

This code is very helpful at initial level.
DMCA.com