
- 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 - if...else if Ladder
- C - Nested if statements
- C - Switch statement
- C - Nested switch statements
- C - Switch Case Using Range
- 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 - Function Prototype
- 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
- C - Predefined Identifier __func__
- 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
- C - Dynamic Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Character Arrays
- 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 - Array of 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 - Const Pointers & Pointer to Const
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Restrict Keyword
- 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
Array of Function Pointers in C
In C, a function pointer stores the address of a function. An array of function pointers is a collection of these pointers that can hold multiple function addresses in an array and we can call any function using its array index. In this chapter, we will learn about −
Function Pointer in C
A function pointer is a pointer that stores the address of a function. Instead of using the function's name, we can use the pointer to call the function directly.
Syntax of Function Pointer
Following is the syntax to declare a function pointer −
return_type (*pointer_name)(parameter_list);
Here, return_type is the type of value the function returns, pointer_name is the name of the pointer, and (parameter_list) shows the types of values the function takes as input.
Example of Function Pointers
Here's a example of a function pointer. We create an add function that takes two int arguments and returns an int. We store its address in func_ptr and call it with 3 and 5 as arguments using func_ptr(3, 5).
#include <stdio.h> int add(int a, int b) { return a + b; } int main() { int (*func_ptr)(int, int) = add; // store address of add printf("Result: %d\n", func_ptr(3, 5)); // call add using pointer return 0; }
The output of this program is shown below −
Result: 8
Array of Function Pointers
An array of function pointers is an array where each element is a function pointer, and each pointer stores the address of a different function. Using this array, we can call any function by using the array index.
Syntax of Array of Function Pointers
Following is the syntax to declare an array of function pointers −
return_type (*array_name[size])(parameter_list);
Here, return_type is the type that all functions in the array return. array_name is the name of the array of function pointers. size is the number of function pointers in the array, and (parameter_list) is the type of arguments that all functions in the array must take.
Note that all functions in the same array must have the same return type and parameters.
Example: Array of Funciton pointers
In this example, we define three functions and create an array funcArr to hold their addresses. We assign the add function to funcArr[0], the sub function to funcArr[1], and the mult function to funcArr[2]. We can then call any function using its index, for example, funcArr[1]() calls the sub function.
#include <stdio.h> //first function void add() { printf("Add function\n"); } //second function void sub() { printf("Subtract function\n"); } //third function void mult() { printf("Multiply function\n"); } int main() { void (*funcArr[3])(); // array of 3 function pointers funcArr[0] = add; funcArr[1] = sub; funcArr[2] = mult; funcArr[0](); // calls add funcArr[1](); // calls sub funcArr[2](); // calls mult return 0; }
Below is the output of each function, where each function prints its name.
Add function Subtract function Multiply function
Example: Array of Function Pointers with Parameters
In this example, we use an array of function pointers where each function takes two integers as parameters. Here's how we did it −
- We create three functions add, sub, and mult, each taking two integers as input and returning an integer.
- We declare an array funcArr of function pointers to hold their addresses.
- Each index of the array points to one function: funcArr[0] points to add, funcArr[1] points to sub, and funcArr[2] points to mult.
- Then, we call each function through the array by passing two integers as arguments. For example, funcArr[0](10, 5) calls the add function with inputs 10 and 5.
#include <stdio.h> int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int mult(int a, int b) { return a * b; } int main() { // array of 3 function pointers taking (int, int) and returning int int (*funcArr[3])(int, int) = { add, sub, mult }; printf("Add: %d\n", funcArr[0](10, 5)); // calls add printf("Subtract: %d\n", funcArr[1](10, 5)); // calls sub printf("Multiply: %d\n", funcArr[2](10, 5)); // calls mult return 0; }
The output below shows the results of addition, subtraction, and multiplication for the given inputs −
Add: 15 Subtract: 5 Multiply: 50
Conclusion
In this chapter, we learned about function pointers and arrays of function pointers. A function pointer stores the address of a function, while an array of function pointers keeps multiple function pointers in one place, and we can call a function using array[index](arguments).