
- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if statement
- C - if...else statement
- C - Nested if statements
- C - Switch statement
- C - Nested switch statements
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- Structures and Unions in C
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Function Pointers
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Pointer to an Array
- C - Pointers vs. Multi-dimensional Arrays
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Initialization of Pointer Arrays
- Storage Classes and Qualifiers
- C - Storage Classes
- Memory Management in C
- C - Memory Management
- C - Memory Address
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- File Handling in C
- C - File I/O (File Handling)
- C - Input & Output
- Constants and Literals in C
- C - Macros
- C - Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C Online Compiler
Function Pointers in C
What is Function Pointer in C?
A pointer in C is a variable that stores the address of another variable. Similarly, a variable that stores the address of a function is called a function pointer or a pointer to a function. Function pointers can be useful when you want to call a function dynamically. The mechanism of callback functions in C is dependent on the function pointers.
Function Pointers point to code like normal pointers. In functions pointers, the function's name can be used to get function's address. A function can also be passed as an argument and can be returned from a function.
Declaring a Function Pointer
You should have a function whose function pointer you are going to declare. To declare a function pointer in C, write a declarative statement with the return type, pointer name, and parameter types of the function it points to.
Declaration Syntax
The following is the syntax to declare a function pointer:
function_return_type(*Pointer_name)(function argument list)
Example
Here is a simple hello() function in C −
void hello(){ printf("Hello World"); }
We declare a pointer to this function as follows −
void (*ptr)() = &hello;
We can now call the function with the help of this function pointer "(*ptr)();".
Function Pointer Example
The following example demonstrates how you can declare and use a function pointer to call a function.
#include <stdio.h> // Defining a function void hello() { printf("Hello World"); } // The main code int main() { // Declaring a function pointer void (*ptr)() = &hello; // Calling function using the // function poiter (*ptr)(); return 0; }
Output
When you run this code, it will produce the following output −
Hello World
Note: Unlike normal pointers which are data pointers, a function pointer points to code. We can use the name of the function as its address (as in case of an array). Hence, the pointer to the function hello() can also be declared as follows −
void (*ptr)() = hello;
Function Pointer with Arguments
A function pointer can also be declared for the function having arguments. During the declaration of the function, you need to provide the specific data types as the parameters list.
Understanding Function Pointer with Arguments
Suppose we have a function called addition() with two arguments −
int addition (int a, int b){ return a + b; }
To declare a function pointer for the above function, we use two arguments −
int (*ptr)(int, int) = addition;
We can then call the function through its pointer, by passing the required arguments −
int z = (*ptr)(x, y);
Try the complete code as below −
Example of Function Pointer with Arguments
Here is the complete code. It shows how you can call a function through its pointer −
#include <stdio.h> int addition (int a, int b){ return a + b; } int main(){ int (*ptr)(int, int) = addition; int x = 10, y = 20; int z = (*ptr)(x, y); printf("Addition of x: %d and y: %d = %d", x, y, z); return 0; }
Output
When you run this code, it will produce the following output −
Addition of x: 10 and y: 20 = 30
Pointer to Function with Pointer Arguments
We can also declare a function pointer when the host function itself as pointer arguments. Let us look at this example −
Understanding Pointer to Function with Pointer Arguments
We have a swap() function that interchanges the values of "x" and "y" with the help of their pointers −
void swap(int *a, int *b){ int c; c = *a; *a = *b; *b = c; }
By following the syntax of declaring a function pointer, it can be declared as follows −
void (*ptr)(int *, int *) = swap;
To swap the values of "x" and "y", pass their pointers to the above function pointer −
(*ptr)(&x, &y);
Example of Pointer to Function with Pointer Arguments
Here is the full code of this example −
#include <stdio.h> void swap(int *a, int *b){ int c; c = *a; *a = *b; *b = c; } int main(){ void (*ptr)(int *, int *) = swap; int x = 10, y = 20; printf("Values of x: %d and y: %d before swap\n", x, y); (*ptr)(&x, &y); printf("Values of x: %d and y: %d after swap", x, y); return 0; }
Output
Values of x: 10 and y: 20 before swap Values of x: 20 and y: 10 after swap
Array of Function Pointers
You can also declare an array of function pointers as per the following syntax:
type (*ptr[])(args) = {fun1, fun2, ...};
Example of Array of Function Pointers
We can use the property of dynamically calling the function through the pointers instead of if-else or switch-case statements. Take a look at the following example −
#include <stdio.h> float add(int a, int b){ return a + b; } float subtract(int a, int b){ return a - b; } float multiply(int a, int b){ return a * b; } float divide(int a, int b){ return a / b; } int main(){ float (*ptr[])(int, int) = {add, subtract, multiply, divide}; int a = 15, b = 10; // 1 for addition, 2 for subtraction // 3 for multiplication, 4 for division int op = 3; if (op > 5) return 0; printf("Result: %.2f", (*ptr[op-1])(a, b)); return 0; }
Output
Run the code and check its output −
Result: 150.00
Change the value of op variable to 1, 2 or 4 to get the results of other functions.