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

PPTX
chapter-7 slide.pptx
PPT
presentation_pointers_1444076066_140676 (1).ppt
PPT
c program.ppt
PPT
Pointers C programming
PPTX
Classes function overloading
PPT
Advanced pointers
PPTX
pointers.pptx
PPTX
Pointers.pptx
chapter-7 slide.pptx
presentation_pointers_1444076066_140676 (1).ppt
c program.ppt
Pointers C programming
Classes function overloading
Advanced pointers
pointers.pptx
Pointers.pptx

Similar to C-Programming Function pointers.pptx (20)

PPT
Lap trinh C co ban va nang cao
PDF
PSPC--UNIT-5.pdf
PPTX
Pointer.pptx
PDF
Functions and Pointers in C: Achieving Modularity and Memory-Level Access Thr...
PPTX
Pointer in C and its significance, Relationship between pointers and memory a...
PDF
CSEG1001 Unit 4 Functions and Pointers
PPT
358 33 powerpoint-slides_3-pointers_chapter-3
PDF
Lk module5 pointers
PPT
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
PPTX
UNIT 4 POINTERS.pptx pointers pptx for basic c language
PPT
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
PPT
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
PPTX
C concepts and programming examples for beginners
PPTX
Function Pointer
PPTX
Pointer in C
PPT
l7-pointers.ppt
PPT
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
PPT
pointer, structure ,union and intro to file handling
PPTX
POINTERS in C language:- Mastering-Pointers-in-C
Lap trinh C co ban va nang cao
PSPC--UNIT-5.pdf
Pointer.pptx
Functions and Pointers in C: Achieving Modularity and Memory-Level Access Thr...
Pointer in C and its significance, Relationship between pointers and memory a...
CSEG1001 Unit 4 Functions and Pointers
358 33 powerpoint-slides_3-pointers_chapter-3
Lk module5 pointers
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
UNIT 4 POINTERS.pptx pointers pptx for basic c language
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
C concepts and programming examples for beginners
Function Pointer
Pointer in C
l7-pointers.ppt
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
POINTERS in C language:- Mastering-Pointers-in-C
Ad

More from LECO9 (20)

PPT
C Programming Intro.ppt
PPTX
Embedded Systems.pptx
PPTX
Basic Electronics.pptx
PPTX
Intro to Microcontroller.pptx
PPTX
PIC_Intro.pptx
PPTX
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
PPTX
STACKS AND QUEUES.pptx
PPTX
UNIONS IN C.pptx
PPTX
Processes, Threads.pptx
PPTX
OPERATORS IN C.pptx
PPTX
DATA STRUCTURES AND LINKED LISTS IN C.pptx
PPTX
FUNCTIONS IN C.pptx
PPTX
DESIGN PATTERN.pptx
PPTX
INTER PROCESS COMMUNICATION (IPC).pptx
PPTX
cprogramming Structures.pptx
PPTX
POINTERS.pptx
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
PPTX
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
PPTX
cprogramming strings.pptx
PPTX
C-Programming Control statements.pptx
C Programming Intro.ppt
Embedded Systems.pptx
Basic Electronics.pptx
Intro to Microcontroller.pptx
PIC_Intro.pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
STACKS AND QUEUES.pptx
UNIONS IN C.pptx
Processes, Threads.pptx
OPERATORS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
FUNCTIONS IN C.pptx
DESIGN PATTERN.pptx
INTER PROCESS COMMUNICATION (IPC).pptx
cprogramming Structures.pptx
POINTERS.pptx
DYNAMIC MEMORY ALLOCATION.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
cprogramming strings.pptx
C-Programming Control statements.pptx
Ad

Recently uploaded (20)

PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Geodesy 1.pptx...............................................
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Well-logging-methods_new................
PDF
PPT on Performance Review to get promotions
PPTX
OOP with Java - Java Introduction (Basics)
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
web development for engineering and engineering
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPT
Mechanical Engineering MATERIALS Selection
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Welding lecture in detail for understanding
bas. eng. economics group 4 presentation 1.pptx
Geodesy 1.pptx...............................................
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Well-logging-methods_new................
PPT on Performance Review to get promotions
OOP with Java - Java Introduction (Basics)
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Model Code of Practice - Construction Work - 21102022 .pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
additive manufacturing of ss316l using mig welding
Foundation to blockchain - A guide to Blockchain Tech
web development for engineering and engineering
Embodied AI: Ushering in the Next Era of Intelligent Systems
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Mechanical Engineering MATERIALS Selection
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Welding lecture in detail for understanding

C-Programming Function pointers.pptx

  • 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);