C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In C programming, a function pointer is a powerful feature that allows you to store the address of a function in a variable. Function pointers enable you to call functions indirectly, pass functions as arguments to other functions, and implement callback mechanisms. This tutorial will explain the basics of function pointers, including their syntax, how to declare and use them, and an example.
The syntax for declaring a function pointer is as follows:
return_type (*function_pointer_name)(parameter_list);
return_type
: The data type of the value returned by the function.function_pointer_name
: The name of the function pointer variable.parameter_list
: A comma-separated list of parameters with their data types, similar to the function definition.Here's an example of declaring a function pointer:
int (*function_ptr)(int, int);
In this example, we have declared a function pointer named function_ptr
that can point to functions taking two int
parameters and returning an int
.
To assign the address of a function to a function pointer, you can use the function name without the parentheses:
function_ptr = &function_name;
or just:
function_ptr = function_name;
Here's an example:
#include <stdio.h> int add(int a, int b) { return a + b; } int main() { int (*function_ptr)(int, int); function_ptr = add; int result = function_ptr(5, 10); printf("The sum is %d\n", result); return 0; }
In this example, we assign the address of the add
function to the function_ptr
function pointer and then call the function through the function pointer.
To call a function through a function pointer, use the function pointer name followed by a set of parentheses containing the argument values:
function_pointer_name(argument_list);
In the previous example, we called the add
function through the function_ptr
function pointer as follows:
int result = function_ptr(5, 10);
Function pointers can be used as arguments to other functions, enabling you to implement callback mechanisms or create more flexible code structures. Here's an example:
#include <stdio.h> int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; } int perform_operation(int a, int b, int (*operation)(int, int)) { return operation(a, b); } int main() { int a = 20, b = 10; int result1 = perform_operation(a, b, add); printf("The sum is %d\n", result1); int result2 = perform_operation(a, b, subtract); printf("The difference is %d\n", result2); return 0; }
In this example, we've defined an add
function and a subtract
function. The perform_operation
function takes two integers and a function pointer as arguments, and it calls the function passed through the function pointer. In the main
function, we use the perform_operation
function to perform addition and subtraction using the add
and subtract
functions, respectively.
Using Function Pointers for Dynamic Behavior in C:
#include <stdio.h> // Function prototype void sayHello() { printf("Hello, Function Pointer!\n"); } int main() { // Declaring a function pointer void (*functionPointer)(); // Assigning the function to the pointer functionPointer = sayHello; // Calling the function using the pointer functionPointer(); return 0; }
Function pointers in C allow you to create dynamic behavior by assigning different functions to a pointer at runtime.
C Code Examples with Function Pointers:
#include <stdio.h> // Function prototypes void add(int a, int b); void subtract(int a, int b); int main() { // Function pointer declaration void (*operation)(int, int); // Assigning different functions to the pointer operation = add; operation(5, 3); operation = subtract; operation(5, 3); return 0; } // Function definitions void add(int a, int b) { printf("Sum: %d\n", a + b); } void subtract(int a, int b) { printf("Difference: %d\n", a - b); }
Function pointers can be used to switch between different behaviors at runtime, providing flexibility in program design.
Passing Function Pointers as Arguments in C:
#include <stdio.h> // Function prototypes void performOperation(int a, int b, void (*operation)(int, int)); // Operation functions void add(int a, int b) { printf("Sum: %d\n", a + b); } void subtract(int a, int b) { printf("Difference: %d\n", a - b); } int main() { // Passing function pointers as arguments performOperation(5, 3, add); performOperation(5, 3, subtract); return 0; } // Function definition void performOperation(int a, int b, void (*operation)(int, int)) { operation(a, b); }
Function pointers can be passed as arguments to other functions, allowing for more dynamic and modular code.
Array of Function Pointers in C Programming:
#include <stdio.h> // Function prototypes void add(int a, int b); void subtract(int a, int b); int main() { // Array of function pointers void (*operations[2])(int, int) = {add, subtract}; // Using the array to call different functions operations[0](5, 3); operations[1](5, 3); return 0; } // Function definitions void add(int a, int b) { printf("Sum: %d\n", a + b); } void subtract(int a, int b) { printf("Difference: %d\n", a - b); }
Arrays of function pointers provide a convenient way to organize and access multiple functions.
Callback Functions and Function Pointers in C:
#include <stdio.h> // Callback function prototype typedef void (*Callback)(int); // Function prototype void performCallback(int value, Callback callback); // Callback function void printSquare(int value) { printf("Square: %d\n", value * value); } int main() { // Using a callback function with a function pointer performCallback(5, printSquare); return 0; } // Function definition void performCallback(int value, Callback callback) { callback(value); }
Callback functions, implemented using function pointers, allow you to pass functions as parameters to other functions.
Function Pointers vs Regular Function Calls in C:
#include <stdio.h> // Function prototype void sayHello() { printf("Hello, Regular Function Call!\n"); } int main() { // Regular function call sayHello(); // Function pointer void (*functionPointer)() = sayHello; // Function pointer call functionPointer(); return 0; }
While regular function calls are direct, function pointers provide flexibility and enable dynamic behavior.
Pointers to Member Functions in C:
#include <stdio.h> // Structure definition struct MathOperations { int (*add)(int, int); int (*subtract)(int, int); }; // Function prototypes int add(int a, int b); int subtract(int a, int b); int main() { // Initializing structure with function pointers struct MathOperations mathOps = {add, subtract}; // Using function pointers from the structure printf("Sum: %d\n", mathOps.add(5, 3)); printf("Difference: %d\n", mathOps.subtract(5, 3)); return 0; } // Function definitions int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; }
Pointers to member functions enable the use of function pointers within structures.