SlideShare a Scribd company logo
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

What's hot

Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
 
Types of function call
Types of function callTypes of function call
Types of function call
ArijitDhali
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
Sathish Narayanan
 
C++
C++C++
File in C language
File in C languageFile in C language
File in C language
Manash Kumar Mondal
 
Pointer in c
Pointer in cPointer in c
Pointer in c
Imamul Kadir
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
lavanya marichamy
 
Call by value
Call by valueCall by value
Call by valueDharani G
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
Ashok Raj
 
C functions
C functionsC functions
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
Vraj Patel
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
Mauryasuraj98
 
Structure in c
Structure in cStructure in c
Structure in c
Prabhu Govind
 
Python-Inheritance.pptx
Python-Inheritance.pptxPython-Inheritance.pptx
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
Rajab Ali
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++

What's hot (20)

Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Types of function call
Types of function callTypes of function call
Types of function call
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Function in C program
Function in C programFunction in C program
Function in C program
 
C++
C++C++
C++
 
File in C language
File in C languageFile in C language
File in C language
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Call by value
Call by valueCall by value
Call by value
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
C functions
C functionsC functions
C functions
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Structure in c
Structure in cStructure in c
Structure in c
 
Python-Inheritance.pptx
Python-Inheritance.pptxPython-Inheritance.pptx
Python-Inheritance.pptx
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 

Similar to Pointer to function 1

Pointer to function 2
Pointer to function 2Pointer to function 2
Pointer to function 2
Abu Bakr Ramadan
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphism
ramya marichamy
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 
Pointers
PointersPointers
Pointers
Samsil Arefin
 
Complicated declarations in c
Complicated declarations in cComplicated declarations in c
Complicated declarations in c
Rahul Budholiya
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
NehaSpillai1
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
Praveen M Jigajinni
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
Functions
FunctionsFunctions
Functions
PatriciaPabalan
 
C function
C functionC function
C function
thirumalaikumar3
 
GE3151 UNIT II Study material .pdf
GE3151 UNIT II Study material .pdfGE3151 UNIT II Study material .pdf
GE3151 UNIT II Study material .pdf
Asst.prof M.Gokilavani
 
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONSINTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
KalaivaniD12
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
ethiouniverse
 
Advanced C programming
Advanced C programmingAdvanced C programming
Advanced C programming
Claus Wu
 
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptbtech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
chintuyadav19
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
Pointer in C
Pointer in CPointer in C
Pointer in C
bipchulabmki
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
s170883BesiVyshnavi
 

Similar to Pointer to function 1 (20)

Pointer to function 2
Pointer to function 2Pointer to function 2
Pointer to function 2
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphism
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Pointers
PointersPointers
Pointers
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
Complicated declarations in c
Complicated declarations in cComplicated declarations in c
Complicated declarations in c
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
Functions
FunctionsFunctions
Functions
 
Function
FunctionFunction
Function
 
C function
C functionC function
C function
 
GE3151 UNIT II Study material .pdf
GE3151 UNIT II Study material .pdfGE3151 UNIT II Study material .pdf
GE3151 UNIT II Study material .pdf
 
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONSINTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
 
Advanced C programming
Advanced C programmingAdvanced C programming
Advanced C programming
 
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptbtech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 

More from Abu Bakr Ramadan

Real time operating systems (rtos) concepts 4
Real time operating systems (rtos) concepts 4Real time operating systems (rtos) concepts 4
Real time operating systems (rtos) concepts 4
Abu Bakr Ramadan
 
Real time operating systems (rtos) concepts 3
Real time operating systems (rtos) concepts 3Real time operating systems (rtos) concepts 3
Real time operating systems (rtos) concepts 3
Abu Bakr Ramadan
 
Real time operating systems (rtos) concepts 9
Real time operating systems (rtos) concepts 9Real time operating systems (rtos) concepts 9
Real time operating systems (rtos) concepts 9
Abu Bakr Ramadan
 
Real time operating systems (rtos) concepts 8
Real time operating systems (rtos) concepts 8Real time operating systems (rtos) concepts 8
Real time operating systems (rtos) concepts 8
Abu Bakr Ramadan
 
Real time operating systems (rtos) concepts 7
Real time operating systems (rtos) concepts 7Real time operating systems (rtos) concepts 7
Real time operating systems (rtos) concepts 7
Abu Bakr Ramadan
 
Real time operating systems (rtos) concepts 5
Real time operating systems (rtos) concepts 5Real time operating systems (rtos) concepts 5
Real time operating systems (rtos) concepts 5
Abu Bakr Ramadan
 
Real time operating systems (rtos) concepts 1
Real time operating systems (rtos) concepts 1Real time operating systems (rtos) concepts 1
Real time operating systems (rtos) concepts 1
Abu Bakr Ramadan
 

More from Abu Bakr Ramadan (7)

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

Recently uploaded

TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
Sharepoint Designs
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
XfilesPro
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
NaapbooksPrivateLimi
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
Jelle | Nordend
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
ayushiqss
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 

Recently uploaded (20)

TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 

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

  1. . 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.