- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- 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 - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Function Pointers
![]() Share with a Friend |
C Programming - C Function Pointers
C Function Pointers
A function pointer in C is a pointer that points to the address of a function. This allows functions to be passed as arguments to other functions, enabling dynamic function calls, creating callback mechanisms, and implementing function tables (useful for event-driven programming, etc.).
Function pointers are extremely powerful because they allow programs to select a function at runtime instead of at compile-time, providing flexibility and extensibility in code design.
Syntax of Function Pointers:
To declare a function pointer, you must specify the return type and the parameters of the function the pointer will point to. The general syntax for declaring a function pointer is:
C
return_type (*pointer_name)(parameter_types);
- return_type: The return type of the function the pointer points to.
- pointer_name: The name of the function pointer.
- parameter_types: The types of parameters that the function accepts.
Example 1: Basic Function Pointer
Let’s start with a simple example that demonstrates how to declare and use function pointers.
C
#include <stdio.h>
// Function to add two integers
int add(int a, int b) {
return a + b;
}
// Function to subtract two integers
int subtract(int a, int b) {
return a - b;
}
int main() {
// Declare a function pointer and assign it to the 'add' function
int (*func_ptr)(int, int);
// Point to the 'add' function
func_ptr = add;
printf("Addition: %d\n", func_ptr(10, 5)); // Calls add(10, 5)
// Point to the 'subtract' function
func_ptr = subtract;
printf("Subtraction: %d\n", func_ptr(10, 5)); // Calls subtract(10, 5)
return 0;
}
Output:
Addition: 15
Subtraction: 5
In this example:
- func_ptr is a function pointer that points to functions accepting two int parameters and returning an int.
- First, func_ptr points to the add function, and then we call it with the arguments 10 and 5.
- Later, we change the pointer to point to the subtract function and call it in the same way.
Example 2: Function Pointer as a Function Argument
Function pointers can be passed as arguments to other functions. This is useful for implementing callback functions.
C
#include <stdio.h>
// Function that performs an operation on two integers using a function pointer
int performOperation(int a, int b, int (*operation)(int, int)) {
return operation(a, b); // Calls the function pointed to by 'operation'
}
// Function to multiply two integers
int multiply(int a, int b) {
return a * b;
}
// Function to add two integers
int add(int a, int b) {
return a + b;
}
int main() {
int result;
// Using function pointer to pass 'add' function
result = performOperation(5, 3, add);
printf("Addition Result: %d\n", result);
// Using function pointer to pass 'multiply' function
result = performOperation(5, 3, multiply);
printf("Multiplication Result: %d\n", result);
return 0;
}
Output:
Addition Result: 8
Multiplication Result: 15
In this example:
- performOperation takes two integers and a function pointer operation, which points to a function that operates on the integers.
- The function pointer allows performOperation to be used with different functions (like add and multiply).
Example 3: Array of Function Pointers
You can create an array of function pointers to handle multiple functions that share the same signature. This is useful when you want to select and execute a function dynamically, such as in a menu system.
C
#include <stdio.h>
// Function to add two integers
int add(int a, int b) {
return a + b;
}
// Function to subtract two integers
int subtract(int a, int b) {
return a - b;
}
// Function to multiply two integers
int multiply(int a, int b) {
return a * b;
}
int main() {
// Array of function pointers
int (*operations[])(int, int) = {add, subtract, multiply};
int a = 10, b = 5;
// Call each function in the array
printf("Addition: %d\n", operations[0](a, b)); // Calls add(10, 5)
printf("Subtraction: %d\n", operations[1](a, b)); // Calls subtract(10, 5)
printf("Multiplication: %d\n", operations[2](a, b)); // Calls multiply(10, 5)
return 0;
}
Output:
Addition: 15
Subtraction: 5
Multiplication: 50
In this example:
- We declare an array operations[] of function pointers, where each pointer points to a function (add, subtract, multiply).
- Each function is called by indexing into the array of pointers.
Example 4: Returning a Function Pointer
Functions can also return a function pointer, which can be used for dynamic function selection.
C
#include <stdio.h>
// Function to add two integers
int add(int a, int b) {
return a + b;
}
// Function to return a pointer to the add function
int (*getAddPointer())(int, int) {
return add; // Returns a pointer to the add function
}
int main() {
int (*ptr)(int, int) = getAddPointer(); // Get pointer to 'add' function
printf("Result of addition: %d\n", ptr(5, 3)); // Calls add(5, 3) through pointer
return 0;
}
Output:
Result of addition: 8
In this example:
- The function getAddPointer returns a pointer to the add function.
- The returned pointer is assigned to ptr, and we use it to call the add function indirectly.
Advantages of Function Pointers:
- Flexibility: You can select which function to call at runtime.
- Callback Mechanism: Function pointers are commonly used in callback functions to allow functions to be passed as arguments (e.g., sorting algorithms).
- Event-driven Programming: Function pointers are useful in event-driven systems like GUI applications, where functions are triggered by events.
Conclusion:
Function pointers are a powerful feature in C that enable dynamic function calls, flexible code, and can be used for callbacks, event handling, and more. They allow functions to be passed around, stored, and invoked at runtime, giving C programs significant flexibility. However, you should handle them carefully, as incorrect use of function pointers can lead to undefined behavior or memory issues.
