Pointers to Functions

 Pointers to Functions


Pointers to functions allow you to store and manipulate function addresses. They can be used to create callbacks and implement advanced programming techniques like function pointers arrays, function pointers as arguments, etc.



#include <stdio.h>


int add(int a, int b) {

    return a + b;

}


int subtract(int a, int b) {

    return a - b;

}


int main() {

    int (*operation)(int, int); // Declare a pointer to a function

    operation = add; // Assign the address of the 'add' function


    int result = operation(5, 3); // Call the function using the pointer

    printf("Result: %d\n", result);


    operation = subtract; // Assign the address of the 'subtract' function

    result = operation(5, 3); // Call the 'subtract' function using the pointer

    printf("Result: %d\n", result);


    return 0;

}

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...