- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- 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 - While loop
- C - For loop
- C - Do...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
- 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 - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- 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
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.