SlideShare a Scribd company logo
1 of 20
 Pointers point to the address of a function
 A running program is allocated a certain space in the main memory
 The executable compiled program code and the used variables
are both put inside this memory
 Thus a function in the program code has an address.
 Can be declared, assigned values, and then used to access the functions
they point to
int add(int,int);
int *add(int,int);
a function add with one argument of int
type and return value of int * (i.e. integer
pointer)
int add(int,int);
int *add(int,int);
int (*add)(int,int);
 Must be initialized prior to use
 Assign the address of a function to a function pointer
Simply uses the name of a function
Optional to use the address operator & in front of the
function’s name
 Must be initialized prior to use
int add(int,int);
fpointer = add;
Names of these functions, add - pointers
to those functions
Two ways
 Use the name of the function pointer instead of the
name of the function
 Explicitly dereference it
result1 = fpointer(4, 5);
Function cannot be
passed as parameter to
another function
#include <stdio.h>
/*Define a pointer to a function */
int(*fpointer)(int, int);
int add(int a, int b){return(a + b);}
int sub(int a, int b) {return(a - b);}
int main(){
fpointer = &add;
printf("%d n", fpointer(4, 5));
fpointer = sub; // Function’s name can also be used to get functions’address
printf(“%d n”, (*fpointer)(6, 2));
return 0;
}
#include <stdio.h>
int add(int a, int b)
{
return a+b;
}
int sub(int a, int b)
{
return a-b;
}
int arith(int f(int,int))
{
int x=5, y=4,s;
s=(*f)(x,y)
return s;
}
int main()
{
printf("n Add: %d",arith(add));
printf("n Sub: %d",arith(sub));
return 0;
}
#include <stdio.h>
int even(int a)
{
if(a%2==0)
return a;
else
return 0;
}
int odd(int a)
{
if(a%2!=0)
return a;
else
return 0;
}
int sum(int f(int), int m, int n)
{
int s = 0;
for(int i=m; i<=n;++i)
s+=f(i); or s+=(*f)(i);
return s;
}
int main()
{
printf("n Sum of Even numbers: %d",sum(even,5,10));
printf("n Sum of Odd numbers: %d",sum(odd,5,10));
return 0;
}
#include <stdio.h>
int add(int x,int y)
{
return x + y;
}
int sub(int x,int y)
{
return x - y;
}
int (*getOperator(const char oper))(int, int)
{
if(oper == '+') return &add;
if(oper == '-') return &sub;
}
int main()
{
int x = 20,y = 10,z = 0;
int (*func)(int, int);
func = getOperator('+');
z = func(x,y);
printf("Add : %dn",z);
func = getOperator('-');
z = func(x,y);
printf("Sub : %dn",z);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef int(*pfOperator)(int, int);
int add(int x,int y)
{
return x + y;
}
int sub(int x,int y)
{
return x - y;
}
pfOperator getOperator(const char oper)
{
if(oper == '+') return &add;
if(oper == '-') return &sub;
}
int main()
{
int x = 20,y = 10,z = 0;
pfOperator func = NULL;
func = getOperator('+');
z = func(x,y);
printf("Add : %dn",z);
func = getOperator('-');
z = func(x,y);
printf("Sub : %dn",z);
return 0;
}
#include <stdio.h>
int(*fpointer[2])(int, int);
int add(int a, int b){return(a + b);}
int sub(int a, int b) {return(a - b);}
int main(){
fpointer[0] = &add;
printf("%d n", (*fpointer[0])(4, 5));
fpointer[1] = sub;
printf(“%d n”, fpointer[1](6, 2));
return 0;}
Function pointer

More Related Content

What's hot (20)

Pointers in c
Pointers in cPointers in c
Pointers in c
 
Structures
StructuresStructures
Structures
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Intoduction to structure
Intoduction to structureIntoduction to structure
Intoduction to structure
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
Pointers
PointersPointers
Pointers
 
pointers
pointerspointers
pointers
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
C programming - Pointer and DMA
C programming - Pointer and DMAC programming - Pointer and DMA
C programming - Pointer and DMA
 
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3
 
Pointers
PointersPointers
Pointers
 
Pointer
PointerPointer
Pointer
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Arrays
ArraysArrays
Arrays
 
C pointer
C pointerC pointer
C pointer
 

Similar to Function pointer (20)

Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
6. function
6. function6. function
6. function
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
Pointers
PointersPointers
Pointers
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
Function in c
Function in cFunction in c
Function in c
 
Function
FunctionFunction
Function
 
Function in c
Function in cFunction in c
Function in c
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
function in c
function in cfunction in c
function in c
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Functions
FunctionsFunctions
Functions
 
Function in c program
Function in c programFunction in c program
Function in c program
 
C function
C functionC function
C function
 
Program presentation
Program presentationProgram presentation
Program presentation
 

More from Gem WeBlog

Analysis of optimization algorithms
Analysis of optimization algorithmsAnalysis of optimization algorithms
Analysis of optimization algorithmsGem WeBlog
 
Particle swarm intelligence
Particle swarm intelligenceParticle swarm intelligence
Particle swarm intelligenceGem WeBlog
 
Nature inspired metaheuristics
Nature inspired metaheuristicsNature inspired metaheuristics
Nature inspired metaheuristicsGem WeBlog
 
Darwin's theory of evolution
Darwin's theory of evolutionDarwin's theory of evolution
Darwin's theory of evolutionGem WeBlog
 
no free-lunch theorem
no free-lunch theoremno free-lunch theorem
no free-lunch theoremGem WeBlog
 
Video and animation
Video and animationVideo and animation
Video and animationGem WeBlog
 
Mpeg video compression
Mpeg video compressionMpeg video compression
Mpeg video compressionGem WeBlog
 
Designing multimedia
Designing multimediaDesigning multimedia
Designing multimediaGem WeBlog
 
Testing and Rolling Out Enterprise Applications
Testing and Rolling Out Enterprise ApplicationsTesting and Rolling Out Enterprise Applications
Testing and Rolling Out Enterprise ApplicationsGem WeBlog
 
Constructing Enterprise Applications
Constructing Enterprise  ApplicationsConstructing Enterprise  Applications
Constructing Enterprise ApplicationsGem WeBlog
 
Architecting and Designing Enterprise Applications
Architecting and Designing Enterprise ApplicationsArchitecting and Designing Enterprise Applications
Architecting and Designing Enterprise ApplicationsGem WeBlog
 
Incepting Enterprise Applications
Incepting Enterprise ApplicationsIncepting Enterprise Applications
Incepting Enterprise ApplicationsGem WeBlog
 
Introduction to BEA
Introduction to BEAIntroduction to BEA
Introduction to BEAGem WeBlog
 
10. NULL pointer
10. NULL pointer10. NULL pointer
10. NULL pointerGem WeBlog
 
13. Pointer and 2D array
13. Pointer  and  2D array13. Pointer  and  2D array
13. Pointer and 2D arrayGem WeBlog
 
12.string and pointer
12.string and pointer12.string and pointer
12.string and pointerGem WeBlog
 
7. Pointer Arithmetic
7. Pointer Arithmetic7. Pointer Arithmetic
7. Pointer ArithmeticGem WeBlog
 
1. Pointer Basic
1. Pointer Basic1. Pointer Basic
1. Pointer BasicGem WeBlog
 
Quick Select - Computing a Median (Decrease and Conquer)
Quick Select - Computing a Median (Decrease and Conquer) Quick Select - Computing a Median (Decrease and Conquer)
Quick Select - Computing a Median (Decrease and Conquer) Gem WeBlog
 

More from Gem WeBlog (20)

Analysis of optimization algorithms
Analysis of optimization algorithmsAnalysis of optimization algorithms
Analysis of optimization algorithms
 
Particle swarm intelligence
Particle swarm intelligenceParticle swarm intelligence
Particle swarm intelligence
 
Nature inspired metaheuristics
Nature inspired metaheuristicsNature inspired metaheuristics
Nature inspired metaheuristics
 
Darwin's theory of evolution
Darwin's theory of evolutionDarwin's theory of evolution
Darwin's theory of evolution
 
no free-lunch theorem
no free-lunch theoremno free-lunch theorem
no free-lunch theorem
 
Video and animation
Video and animationVideo and animation
Video and animation
 
Mpeg video compression
Mpeg video compressionMpeg video compression
Mpeg video compression
 
Digital audio
Digital audioDigital audio
Digital audio
 
Designing multimedia
Designing multimediaDesigning multimedia
Designing multimedia
 
Testing and Rolling Out Enterprise Applications
Testing and Rolling Out Enterprise ApplicationsTesting and Rolling Out Enterprise Applications
Testing and Rolling Out Enterprise Applications
 
Constructing Enterprise Applications
Constructing Enterprise  ApplicationsConstructing Enterprise  Applications
Constructing Enterprise Applications
 
Architecting and Designing Enterprise Applications
Architecting and Designing Enterprise ApplicationsArchitecting and Designing Enterprise Applications
Architecting and Designing Enterprise Applications
 
Incepting Enterprise Applications
Incepting Enterprise ApplicationsIncepting Enterprise Applications
Incepting Enterprise Applications
 
Introduction to BEA
Introduction to BEAIntroduction to BEA
Introduction to BEA
 
10. NULL pointer
10. NULL pointer10. NULL pointer
10. NULL pointer
 
13. Pointer and 2D array
13. Pointer  and  2D array13. Pointer  and  2D array
13. Pointer and 2D array
 
12.string and pointer
12.string and pointer12.string and pointer
12.string and pointer
 
7. Pointer Arithmetic
7. Pointer Arithmetic7. Pointer Arithmetic
7. Pointer Arithmetic
 
1. Pointer Basic
1. Pointer Basic1. Pointer Basic
1. Pointer Basic
 
Quick Select - Computing a Median (Decrease and Conquer)
Quick Select - Computing a Median (Decrease and Conquer) Quick Select - Computing a Median (Decrease and Conquer)
Quick Select - Computing a Median (Decrease and Conquer)
 

Recently uploaded

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 

Recently uploaded (20)

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 

Function pointer

  • 1.
  • 2.  Pointers point to the address of a function  A running program is allocated a certain space in the main memory  The executable compiled program code and the used variables are both put inside this memory  Thus a function in the program code has an address.  Can be declared, assigned values, and then used to access the functions they point to
  • 3.
  • 4. int add(int,int); int *add(int,int); a function add with one argument of int type and return value of int * (i.e. integer pointer)
  • 6.
  • 7.  Must be initialized prior to use  Assign the address of a function to a function pointer Simply uses the name of a function Optional to use the address operator & in front of the function’s name
  • 8.  Must be initialized prior to use int add(int,int); fpointer = add; Names of these functions, add - pointers to those functions
  • 9.
  • 10. Two ways  Use the name of the function pointer instead of the name of the function  Explicitly dereference it result1 = fpointer(4, 5); Function cannot be passed as parameter to another function
  • 11. #include <stdio.h> /*Define a pointer to a function */ int(*fpointer)(int, int); int add(int a, int b){return(a + b);} int sub(int a, int b) {return(a - b);} int main(){ fpointer = &add; printf("%d n", fpointer(4, 5)); fpointer = sub; // Function’s name can also be used to get functions’address printf(“%d n”, (*fpointer)(6, 2)); return 0; }
  • 12.
  • 13. #include <stdio.h> int add(int a, int b) { return a+b; } int sub(int a, int b) { return a-b; } int arith(int f(int,int)) { int x=5, y=4,s; s=(*f)(x,y) return s; } int main() { printf("n Add: %d",arith(add)); printf("n Sub: %d",arith(sub)); return 0; }
  • 14. #include <stdio.h> int even(int a) { if(a%2==0) return a; else return 0; } int odd(int a) { if(a%2!=0) return a; else return 0; } int sum(int f(int), int m, int n) { int s = 0; for(int i=m; i<=n;++i) s+=f(i); or s+=(*f)(i); return s; } int main() { printf("n Sum of Even numbers: %d",sum(even,5,10)); printf("n Sum of Odd numbers: %d",sum(odd,5,10)); return 0; }
  • 15.
  • 16. #include <stdio.h> int add(int x,int y) { return x + y; } int sub(int x,int y) { return x - y; } int (*getOperator(const char oper))(int, int) { if(oper == '+') return &add; if(oper == '-') return &sub; } int main() { int x = 20,y = 10,z = 0; int (*func)(int, int); func = getOperator('+'); z = func(x,y); printf("Add : %dn",z); func = getOperator('-'); z = func(x,y); printf("Sub : %dn",z); return 0; }
  • 17. #include <stdio.h> #include <stdlib.h> typedef int(*pfOperator)(int, int); int add(int x,int y) { return x + y; } int sub(int x,int y) { return x - y; } pfOperator getOperator(const char oper) { if(oper == '+') return &add; if(oper == '-') return &sub; } int main() { int x = 20,y = 10,z = 0; pfOperator func = NULL; func = getOperator('+'); z = func(x,y); printf("Add : %dn",z); func = getOperator('-'); z = func(x,y); printf("Sub : %dn",z); return 0; }
  • 18.
  • 19. #include <stdio.h> int(*fpointer[2])(int, int); int add(int a, int b){return(a + b);} int sub(int a, int b) {return(a - b);} int main(){ fpointer[0] = &add; printf("%d n", (*fpointer[0])(4, 5)); fpointer[1] = sub; printf(“%d n”, fpointer[1](6, 2)); return 0;}