C Programming Tutorial

C Programming Tutorial

Function Pointer in C

Hello Everyone, In this article, we will cover the concept of Function Pointer in C Programming. Function pointers are a powerful feature that allows you to store the address of a function and call it indirectly. This can make your code more flexible and modular. Whether you are new to C or have some experience, this guide will help you to understand basics of Function Pointer in C effectively with examples. Let’s get started.

A function pointer in C is a variable that stores the address of a function in memory, rather than holding a regular data value. This allows the function pointer to be used to call the function indirectly, providing flexibility and enabling more dynamic programming techniques, such as callback functions and function dispatch tables.

Here’s a basic explanation of how to use function pointers in C:

1. Define the function pointer type: To use a function pointer, you need to define its type by specifying the return type and the parameters of the function it will point to.

    typedef return_type (*function_pointer_name)(parameter_type1, parameter_type2, ...);
    

    For example, a function pointer type for a function that takes two integers and returns an integer would be:

    typedef int (*int_function_ptr)(int, int);
    

    2. Assign a function to the function pointer: Once the function pointer type is defined, you can create a function pointer variable and assign a function to it. The function must have the same return type and parameters as the function pointer type.

      int_function_ptr my_function_ptr = &my_function;
      

      3. Call the function through the function pointer: Now that the function pointer has been assigned a function, you can call the function indirectly using the function pointer.

        int result = my_function_ptr(10, 20);
        

        Here’s a complete example:

        #include <stdio.h>// Function to add two integers
        int add(int a, int b) {
            return a + b;
        }
        
        // Function pointer type definition
        typedef int (*int_function_ptr)(int, int);
        
        int main() {
            // Create a function pointer and assign the 'add' function to it
            int_function_ptr my_function_ptr = &add;
        
            // Call the 'add' function indirectly using the function pointer
            int result = my_function_ptr(10, 20);
        
            printf("Result: %d\\n", result); // Output: Result: 30
        
            return 0;
        }
        

        Function pointers can be used to implement various programming techniques, such as passing functions as arguments to other functions or storing an array of function pointers to create a dispatch table for different operations.

        In real life, a function pointer can be thought of as a remote control with multiple buttons, where each button is assigned to perform a specific task (e.g., turn on the TV, change the channel, adjust the volume). In the context of C programming, these tasks are represented by functions, and the function pointer is the “remote control” that can be used to call these functions indirectly.

        Let’s consider a real-life example of an event management system. You have different types of events (birthday party, wedding, conference), and for each event, you need to send a different type of invitation. Function pointers can be used to call the appropriate function to generate the specific invitation based on the event type.

        Here’s a simple implementation of this example in C:

        #include <stdio.h>// Invitation functions for different event types
        void send_birthday_invitation() {
            printf("You are invited to a birthday party!\\n");
        }
        
        void send_wedding_invitation() {
            printf("You are invited to a wedding!\\n");
        }
        
        void send_conference_invitation() {
            printf("You are invited to a conference!\\n");
        }
        
        // Function pointer type definition
        typedef void (*event_invitation_function)();
        
        // Function to send invitations based on the event type
        void send_invitation(int event_type) {
            // Array of function pointers representing the different event types
            event_invitation_function invitation_functions[] = {
                send_birthday_invitation,
                send_wedding_invitation,
                send_conference_invitation
            };
        
            // Call the appropriate invitation function based on the event type
            if (event_type >= 0 && event_type < sizeof(invitation_functions) / sizeof(invitation_functions[0])) {
                invitation_functions[event_type]();
            } else {
                printf("Invalid event type.\\n");
            }
        }
        
        int main() {
            int event_type;
        
            printf("Enter event type (0 - Birthday, 1 - Wedding, 2 - Conference): ");
            scanf("%d", &event_type);
        
            send_invitation(event_type);
        
            return 0;
        }
        

        In this example, we have three different functions for sending invitations for different event types. We define a function pointer type (event_invitation_function) and create an array of function pointers (invitation_functions). The send_invitation() function takes the event type as an input and calls the appropriate function based on the input using the function pointer.

        This approach allows for flexibility and can be easily extended to support more event types in the future without modifying the send_invitation() function.

        Share This Article