SlideShare a Scribd company logo
1 of 10
FUNCTION POINTERS
INTRODUCTION
• Function pointers are pointers that point to functions instead of data types.
• They can be used to allow variability in the function that is to be called, at
run-time.
Syntax:
•returnType (*name)(parameters)
•typedef returntype (*name)(parameters)
•typedef returnType Name(parameters);
Name *name;
•typedef returnType Name(parameters);
typedef Name *NamePtr;
DECLARING A FUNCTION POINTER IN C
Now that we know that functions have a unique memory address, we can
use Function pointers in C that can point to the first executable code
inside a function body.
SYNTAX OF FUNCTION POINTER IN C
return_type (* pointer_name) (datatype_arg_1, datatype_arg_1, ...);
• Declaring a function pointer in C is comparable to declaring a function
except that when a function pointer is declared, we prefix its name which is
an Asterisk * symbol.
• For example, if a function has the declaration
float foo (int, int);
• Declaration Of a function pointer in C for the function foo will be
// function pointer declaration
float (*foo_pointer) (int, int);
/* assigning the address of the function (foo) to function pointer */
foo_pointer = foo;
CALLING A FUNCTION THROUGH A FUNCTION
POINTER IN C
• Calling a function using a pointer is similar to calling a function in the
usual way using the name of the function.
• Suppose we declare a Function and its pointer as given below
int (*pointer) (int); // function pointer declaration
int areaSquare (int); // function declaration
pointer = areaSquare;
To call the function areaSquare, we can create a function call using any of
the three ways
int length = 5;
// Different ways to call the function
// 1. using function name
int area = areaSquare(length);
// 2. using function pointer (a)
int area = (*pointer)(length);
// 3. using function pointer (b)
int area = pointer(length);
•The effect of calling functions using pointers or
using their name is the same.
FUNCTIONS USING POINTER VARIABLES
• C allows pointers to be passed in as function arguments and also return
pointers from the function.
• To pass pointers in the function, we simply declare the function parameter
as pointer type.
• When functions have their pointer type arguments, the changes made on
them inside the function persists even after program exists function scope
because the changes are made on the actual address pointed by the pointer.
• This approach to pass arguments to a function is called as pass by
reference because as shown in the figure below reference of the variable is
passed to the function instead of the value stored in the address.
SAFE WAYS TO RETURN A POINTER FROM A
FUNCTION
1. Return variables are either created using the keyword static or
created dynamically at run time because such variables exist in
memory beyond the scope of the called function.
2. Use arguments that are passed by their reference because such
functions exist in the calling function scope.
FUNCTIONS USING POINTER VARIABLES
• C allows pointers to be passed in as function arguments and also return
pointers from the function.
• To pass pointers in the function, we simply declare the function parameter
as pointer type.
• When functions have their pointer type arguments, the changes made on
them inside the function persists even after program exists function scope
because the changes are made on the actual address pointed by the pointer.
• This approach to pass arguments to a function is called as pass by
reference because as shown in the figure below reference of the variable is
passed to the function instead of the value stored in the address.
REFERENCING AND DEREFERENCING OF
FUNCTION POINTER IN C
• Suppose we want to create a sorting function. It makes more sense to allow
the function’s caller to decide the order in which values are sorted (ascending,
descending, etc).
• One way is to provide a flag in the function argument to decide what to do,
but this is not flexible.
• Another way is to provide user flexibility to pass a function in our sort
function. This function can take two values as input and perform a
comparison between them. A syntax for our new function will look like.
void sort(int array[], int n, function comparison);

More Related Content

Similar to C Function Pointers Guide

CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxGebruGetachew2
 
An Introduction on Functions in python 3.pdf
An Introduction on Functions in python 3.pdfAn Introduction on Functions in python 3.pdf
An Introduction on Functions in python 3.pdfalivaisi1
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdfNehaSpillai1
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functionsnikshaikh786
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++LPU
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and commentMalligaarjunanN
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmigAppili Vamsi Krishna
 
User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.NabeelaNousheen
 
Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiSowmya Jyothi
 

Similar to C Function Pointers Guide (20)

CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
An Introduction on Functions in python 3.pdf
An Introduction on Functions in python 3.pdfAn Introduction on Functions in python 3.pdf
An Introduction on Functions in python 3.pdf
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Functions
FunctionsFunctions
Functions
 
Functions
FunctionsFunctions
Functions
 
3. functions modules_programs (1)
3. functions modules_programs (1)3. functions modules_programs (1)
3. functions modules_programs (1)
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
 
Pointer to function 2
Pointer to function 2Pointer to function 2
Pointer to function 2
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Ch9 Functions
Ch9 FunctionsCh9 Functions
Ch9 Functions
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
pointers (1).ppt
pointers (1).pptpointers (1).ppt
pointers (1).ppt
 
Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothi
 

More from SKUP1

serial_busses_i2c.pptx
serial_busses_i2c.pptxserial_busses_i2c.pptx
serial_busses_i2c.pptxSKUP1
 
DESIGN PATTERN.pptx
DESIGN PATTERN.pptxDESIGN PATTERN.pptx
DESIGN PATTERN.pptxSKUP1
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxSKUP1
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxSKUP1
 
C-Programming File-handling-C.pptx
C-Programming  File-handling-C.pptxC-Programming  File-handling-C.pptx
C-Programming File-handling-C.pptxSKUP1
 
Processes, Threads.pptx
Processes, Threads.pptxProcesses, Threads.pptx
Processes, Threads.pptxSKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxSKUP1
 
Finite State Machine.ppt.pptx
Finite State Machine.ppt.pptxFinite State Machine.ppt.pptx
Finite State Machine.ppt.pptxSKUP1
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxSKUP1
 
cprogramming strings.pptx
cprogramming strings.pptxcprogramming strings.pptx
cprogramming strings.pptxSKUP1
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptxSKUP1
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptxSKUP1
 
cprogramming Structures.pptx
cprogramming Structures.pptxcprogramming Structures.pptx
cprogramming Structures.pptxSKUP1
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptxSKUP1
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSKUP1
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptxSKUP1
 
C MEMORY MODEL​.pptx
C MEMORY MODEL​.pptxC MEMORY MODEL​.pptx
C MEMORY MODEL​.pptxSKUP1
 
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptxDATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptxSKUP1
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxSKUP1
 
COMPILATION PROCESS IN C.pptx
COMPILATION PROCESS IN C.pptxCOMPILATION PROCESS IN C.pptx
COMPILATION PROCESS IN C.pptxSKUP1
 

More from SKUP1 (20)

serial_busses_i2c.pptx
serial_busses_i2c.pptxserial_busses_i2c.pptx
serial_busses_i2c.pptx
 
DESIGN PATTERN.pptx
DESIGN PATTERN.pptxDESIGN PATTERN.pptx
DESIGN PATTERN.pptx
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
 
C-Programming File-handling-C.pptx
C-Programming  File-handling-C.pptxC-Programming  File-handling-C.pptx
C-Programming File-handling-C.pptx
 
Processes, Threads.pptx
Processes, Threads.pptxProcesses, Threads.pptx
Processes, Threads.pptx
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
Finite State Machine.ppt.pptx
Finite State Machine.ppt.pptxFinite State Machine.ppt.pptx
Finite State Machine.ppt.pptx
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
 
cprogramming strings.pptx
cprogramming strings.pptxcprogramming strings.pptx
cprogramming strings.pptx
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
 
cprogramming Structures.pptx
cprogramming Structures.pptxcprogramming Structures.pptx
cprogramming Structures.pptx
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptx
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
 
C MEMORY MODEL​.pptx
C MEMORY MODEL​.pptxC MEMORY MODEL​.pptx
C MEMORY MODEL​.pptx
 
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptxDATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
 
COMPILATION PROCESS IN C.pptx
COMPILATION PROCESS IN C.pptxCOMPILATION PROCESS IN C.pptx
COMPILATION PROCESS IN C.pptx
 

Recently uploaded

The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
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
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
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
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(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
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
(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
 

Recently uploaded (20)

The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
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, ...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
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
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.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...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
(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
 

C Function Pointers Guide

  • 2. INTRODUCTION • Function pointers are pointers that point to functions instead of data types. • They can be used to allow variability in the function that is to be called, at run-time. Syntax: •returnType (*name)(parameters) •typedef returntype (*name)(parameters) •typedef returnType Name(parameters); Name *name; •typedef returnType Name(parameters); typedef Name *NamePtr;
  • 3. DECLARING A FUNCTION POINTER IN C Now that we know that functions have a unique memory address, we can use Function pointers in C that can point to the first executable code inside a function body. SYNTAX OF FUNCTION POINTER IN C return_type (* pointer_name) (datatype_arg_1, datatype_arg_1, ...);
  • 4. • Declaring a function pointer in C is comparable to declaring a function except that when a function pointer is declared, we prefix its name which is an Asterisk * symbol. • For example, if a function has the declaration float foo (int, int); • Declaration Of a function pointer in C for the function foo will be // function pointer declaration float (*foo_pointer) (int, int); /* assigning the address of the function (foo) to function pointer */ foo_pointer = foo;
  • 5. CALLING A FUNCTION THROUGH A FUNCTION POINTER IN C • Calling a function using a pointer is similar to calling a function in the usual way using the name of the function. • Suppose we declare a Function and its pointer as given below int (*pointer) (int); // function pointer declaration int areaSquare (int); // function declaration pointer = areaSquare;
  • 6. To call the function areaSquare, we can create a function call using any of the three ways int length = 5; // Different ways to call the function // 1. using function name int area = areaSquare(length); // 2. using function pointer (a) int area = (*pointer)(length); // 3. using function pointer (b) int area = pointer(length); •The effect of calling functions using pointers or using their name is the same.
  • 7. FUNCTIONS USING POINTER VARIABLES • C allows pointers to be passed in as function arguments and also return pointers from the function. • To pass pointers in the function, we simply declare the function parameter as pointer type. • When functions have their pointer type arguments, the changes made on them inside the function persists even after program exists function scope because the changes are made on the actual address pointed by the pointer. • This approach to pass arguments to a function is called as pass by reference because as shown in the figure below reference of the variable is passed to the function instead of the value stored in the address.
  • 8. SAFE WAYS TO RETURN A POINTER FROM A FUNCTION 1. Return variables are either created using the keyword static or created dynamically at run time because such variables exist in memory beyond the scope of the called function. 2. Use arguments that are passed by their reference because such functions exist in the calling function scope.
  • 9. FUNCTIONS USING POINTER VARIABLES • C allows pointers to be passed in as function arguments and also return pointers from the function. • To pass pointers in the function, we simply declare the function parameter as pointer type. • When functions have their pointer type arguments, the changes made on them inside the function persists even after program exists function scope because the changes are made on the actual address pointed by the pointer. • This approach to pass arguments to a function is called as pass by reference because as shown in the figure below reference of the variable is passed to the function instead of the value stored in the address.
  • 10. REFERENCING AND DEREFERENCING OF FUNCTION POINTER IN C • Suppose we want to create a sorting function. It makes more sense to allow the function’s caller to decide the order in which values are sorted (ascending, descending, etc). • One way is to provide a flag in the function argument to decide what to do, but this is not flexible. • Another way is to provide user flexibility to pass a function in our sort function. This function can take two values as input and perform a comparison between them. A syntax for our new function will look like. void sort(int array[], int n, function comparison);