SlideShare a Scribd company logo
4
Most read
6
Most read
9
Most read
Abu Bakr Mohamed Ramadan
eng.abubakr@gmail.com
Content:
 What is function pointer?
 Motivation, what is the use cases of function pointer?
 Declaration of function pointer in c
 Initialization of function pointer in c
 Calling a function using the function pointer
 Example on function pointer
 Function pointer as arguments
What is function pointer?
 A function pointer is one of the most important pointer tools which is
often ignored and misunderstood by the people.
 A function pointer is similar to the other pointers but the only difference is
that it points to a function instead of the variable.
 Unlike normal pointers, a function pointer points to code, not data. Typically a
function pointer stores the start of executable code.
Motivation, what is the use cases of function pointer?
 Interrupt Vector table is an array of pointers to function (ISR),
 Callback mechanism in modular layered application,
 Basic concept in OS and RTOS programing,
 we can achieve OOP concepts like inheritance and polymorphism in C
using function pointers.
Declaration of function pointer in c
 int fpData(int);
 int *fpData(int);
 int (*fpData)(int);
 int (*fpData)(char *);
 int* (*fpData)(char *);
 int (*fpData)(int, char *);
Initialization of function pointer in c
 declaration of function pointer
int (* pfAddTwoNumber) (int, int);
 Initialize the function pointer with the address of AddTwoNumber
pfAddTwoNumber = & AddTwoNumber;
or
pfAddTwoNumber = AddTwoNumber;
 We can also initialize the function pointer in a single line.
int (* pfAddTwoNumber) (int, int) = AddTwoNumber;
Calling a function using the function pointer
 First, write the name of a function pointer.
pfAddTwoNumber;
 We know that function pointer is also similar to another pointer but the only difference is
that function pointer pointed to a function except for the variable. So we put the * as a
prefix to the name of function pointer to take the value of the function pointer.
*pfAddTwoNumber;
 Finally, we have a function. For the function calling just simply add the arguments list
with extra parenthesis to the above expression.
(*pfAddTwoNumber)(10, 20);
Example
#include <stdio.h>
#include <stdlib.h>
//function used to add two numbers
int AddTwoNumber(int iData1 ,int iData2)
{
return (iData1 + iData2);
}
int main(int argc, char *argv[])
{
int iRetValue = 0;
//Declaration of function pointer
int (*pfAddTwoNumber)(int,int) = NULL;
//initialise the function pointer
pfAddTwoNumber = AddTwoNumber;
//Calling the function using function pointer
iRetValue = (*pfAddTwoNumber)(10,20);
//display addition of two number
printf("nnAddition of two number = %dnn",iRetValue);
return 0; }
Function pointer as arguments
 we can pass the function pointer as an argument into the function like
the other pointers.
 Example on Function pointer as arguments

More Related Content

PPTX
Dynamic memory allocation in c
PPTX
Array Of Pointers
PDF
Python strings
PPTX
Pointers in c++
PDF
C Pointers
PPTX
Function Pointer
PPT
Function overloading(c++)
PPTX
Pointers in c++
Dynamic memory allocation in c
Array Of Pointers
Python strings
Pointers in c++
C Pointers
Function Pointer
Function overloading(c++)
Pointers in c++

What's hot (20)

PPTX
Constructor and Types of Constructors
PPTX
Python Functions
PPTX
Pointers in C Programming
PPT
structure and union
PPTX
C++ string
PDF
Strings in python
PPTX
Constructor in java
PPTX
Pointers,virtual functions and polymorphism cpp
PPTX
PPTX
Array Introduction One-dimensional array Multidimensional array
PPSX
Files in c++
PPT
pre processor directives in C
PPTX
Chapter 07 inheritance
PPTX
Data types in c++
PPTX
Inline function
PPTX
Pointers in c++
PPTX
INLINE FUNCTION IN C++
PPTX
data types in C programming
PPTX
arrays and pointers
PPTX
Pointers in C
Constructor and Types of Constructors
Python Functions
Pointers in C Programming
structure and union
C++ string
Strings in python
Constructor in java
Pointers,virtual functions and polymorphism cpp
Array Introduction One-dimensional array Multidimensional array
Files in c++
pre processor directives in C
Chapter 07 inheritance
Data types in c++
Inline function
Pointers in c++
INLINE FUNCTION IN C++
data types in C programming
arrays and pointers
Pointers in C
Ad

Similar to Pointer to function 1 (20)

PPT
Advanced pointers
PDF
c programming
PDF
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
PDF
The Function Pointer Tutorials
PPTX
Pointer to function 2
PPTX
Function pointer
PPTX
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
PPT
presentation_pointers_1444076066_140676 (1).ppt
PPT
c program.ppt
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
PPTX
Pointer in C
PDF
PSPC--UNIT-5.pdf
PPT
Lap trinh C co ban va nang cao
PPTX
C-Programming Function pointers.pptx
PPTX
C-Programming Function pointers.pptx
PPTX
pointers.pptx
PDF
C Recursion, Pointers, Dynamic memory management
PPTX
Pointers and single &multi dimentionalarrays.pptx
PPT
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
Advanced pointers
c programming
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
The Function Pointer Tutorials
Pointer to function 2
Function pointer
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
presentation_pointers_1444076066_140676 (1).ppt
c program.ppt
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Pointer in C
PSPC--UNIT-5.pdf
Lap trinh C co ban va nang cao
C-Programming Function pointers.pptx
C-Programming Function pointers.pptx
pointers.pptx
C Recursion, Pointers, Dynamic memory management
Pointers and single &multi dimentionalarrays.pptx
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
Ad

More from Abu Bakr Ramadan (7)

PPTX
Real time operating systems (rtos) concepts 4
PPTX
Real time operating systems (rtos) concepts 3
PPTX
Real time operating systems (rtos) concepts 9
PPTX
Real time operating systems (rtos) concepts 8
PPTX
Real time operating systems (rtos) concepts 7
PPTX
Real time operating systems (rtos) concepts 5
PPTX
Real time operating systems (rtos) concepts 1
Real time operating systems (rtos) concepts 4
Real time operating systems (rtos) concepts 3
Real time operating systems (rtos) concepts 9
Real time operating systems (rtos) concepts 8
Real time operating systems (rtos) concepts 7
Real time operating systems (rtos) concepts 5
Real time operating systems (rtos) concepts 1

Recently uploaded (20)

PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Digital Strategies for Manufacturing Companies
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Essential Infomation Tech presentation.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
ai tools demonstartion for schools and inter college
PPTX
L1 - Introduction to python Backend.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Digital Strategies for Manufacturing Companies
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
CHAPTER 2 - PM Management and IT Context
How to Migrate SBCGlobal Email to Yahoo Easily
Essential Infomation Tech presentation.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Design an Analysis of Algorithms I-SECS-1021-03
How Creative Agencies Leverage Project Management Software.pdf
Nekopoi APK 2025 free lastest update
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
ai tools demonstartion for schools and inter college
L1 - Introduction to python Backend.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
wealthsignaloriginal-com-DS-text-... (1).pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Operating system designcfffgfgggggggvggggggggg
Navsoft: AI-Powered Business Solutions & Custom Software Development

Pointer to function 1

  • 1. Abu Bakr Mohamed Ramadan eng.abubakr@gmail.com
  • 2. Content:  What is function pointer?  Motivation, what is the use cases of function pointer?  Declaration of function pointer in c  Initialization of function pointer in c  Calling a function using the function pointer  Example on function pointer  Function pointer as arguments
  • 3. What is function pointer?  A function pointer is one of the most important pointer tools which is often ignored and misunderstood by the people.  A function pointer is similar to the other pointers but the only difference is that it points to a function instead of the variable.  Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code.
  • 4. Motivation, what is the use cases of function pointer?  Interrupt Vector table is an array of pointers to function (ISR),  Callback mechanism in modular layered application,  Basic concept in OS and RTOS programing,  we can achieve OOP concepts like inheritance and polymorphism in C using function pointers.
  • 5. Declaration of function pointer in c  int fpData(int);  int *fpData(int);  int (*fpData)(int);  int (*fpData)(char *);  int* (*fpData)(char *);  int (*fpData)(int, char *);
  • 6. Initialization of function pointer in c  declaration of function pointer int (* pfAddTwoNumber) (int, int);  Initialize the function pointer with the address of AddTwoNumber pfAddTwoNumber = & AddTwoNumber; or pfAddTwoNumber = AddTwoNumber;  We can also initialize the function pointer in a single line. int (* pfAddTwoNumber) (int, int) = AddTwoNumber;
  • 7. Calling a function using the function pointer  First, write the name of a function pointer. pfAddTwoNumber;  We know that function pointer is also similar to another pointer but the only difference is that function pointer pointed to a function except for the variable. So we put the * as a prefix to the name of function pointer to take the value of the function pointer. *pfAddTwoNumber;  Finally, we have a function. For the function calling just simply add the arguments list with extra parenthesis to the above expression. (*pfAddTwoNumber)(10, 20);
  • 8. Example #include <stdio.h> #include <stdlib.h> //function used to add two numbers int AddTwoNumber(int iData1 ,int iData2) { return (iData1 + iData2); } int main(int argc, char *argv[]) { int iRetValue = 0; //Declaration of function pointer int (*pfAddTwoNumber)(int,int) = NULL; //initialise the function pointer pfAddTwoNumber = AddTwoNumber; //Calling the function using function pointer iRetValue = (*pfAddTwoNumber)(10,20); //display addition of two number printf("nnAddition of two number = %dnn",iRetValue); return 0; }
  • 9. Function pointer as arguments  we can pass the function pointer as an argument into the function like the other pointers.  Example on Function pointer as arguments

Editor's Notes

  • #6: . It seems like difficult in beginning but once you are familiar with function pointer then it becomes easy. void ( *pfDisplayMessage) (const char *); In above expression, pfDisplayMessage is a pointer to a function taking one argument, const char *, and returns void.