Thread: Function pointer doesn't work as expected

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    97

    Function pointer doesn't work as expected

    Hi, I try to understand the concept of function pointers and I wrote an example for demonstration but it doesn't work as expected.

    The return_error fuction never calls the mul_numbers.

    Code:
    #include <stdio.h>
    #include <stdint.h>
    
    
    typedef enum
    {
        status_ok = 1,        // Operation terminated succesfully
        status_hw_error,      // Hardware error
        status_busy,          // Procedure in progress
        status_invalid_param, // Invalid parameter
        status_unknown_error  // Unknown error
    } status_t;
    
    
    int add_numbers(int x, int y);
    int mul_numbers(int x, int y);
    
    
    typedef int (*operation_p)(int, int); // Declare a funciton pointer
    
    
    status_t return_error(operation_p operation, int x, int y); // Function declaration (signature)
    
    
    int main()
    {
        int a = 5;
        int b = 3;
    
        status_t error_code;
        operation_p add; // Create a variable of type operation_p
        operation_p mult; // Create a variable of type operation_p
    
        add = add_numbers;
        printf("Address of add: %d\n", &add);
        error_code = return_error(add, a, b);
        printf("Error: %d\n", error_code);
    
        mult = mul_numbers;
        printf("Address of mul %d\n", &mult);
        error_code = return_error(mult, a, b);
        printf("Error: %d\n", error_code);
    
        return 0;
    }
    
    
    status_t return_error(operation_p operation, int x, int y)
    {
        printf("Address of operation: %d\n", &operation);
    
    
        if (operation = &add_numbers)
        {
            printf("Addition callback error code\n");
            if (operation(x, y) > 40)
            {
                return status_ok;
            }
            else if (operation(x, y) < 10)
            {
                return status_hw_error;
            }
            else
            {
                return status_unknown_error;
            }
        }
        else if (operation = &mul_numbers)
        {
            printf("Multiply callback error code \n");
    
    
            if (operation(x, y) < 30)
            {
    
    
                return status_ok;
            }
            else
            {
                return status_unknown_error;
            }
        }
    }
    
    
    int add_numbers(int x, int y)
    {
        return x + y;
    }
    
    
    int mul_numbers(int x, int y)
    {
        return x * y;
    }
    This is the printf output. The address of "operation" is always the same and differs from the addresses of "add" and "mul'

    https://cboard.cprogramming.com/imag...BJRU5ErkJggg==

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    A function call is an operation, and one of its operands is the pointer to the function to be called (the other are a list of arguments). When you do
    Code:
    int f( int x, int a ) { return a + b; }
    You are telling the compiler to add both arguments and return the result, but you are also teliing that the entry point for that function is called f. This identifier is the address of the entry point, it is a pointer.

    When you do:
    Code:
    int (*fptr)(int);
    To initialize this pointer you do:
    Code:
    fptr = f;
    Not:
    Code:
    fptr = &f
    This is evident, since to call this pointed function you use the same syntax:
    Code:
    y = fptr( n );
    To compare the pointers you do
    Code:
    if ( fptr == f ) ...
    Not:
    Code:
    if ( fptr = &f ) ...
    BTW: To compare you use ==, not =.

    And to print the address in a pointer you use %p, not %d, like in:
    Code:
    status_t return_error(operation_p operation, int x, int y)
    {
      printf("Address of operation: %p\n", operation);  // notice: without &

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    Thank a lot!!! It works fine now. I know about == operator, it was just carelessness.

    But I didn't know about that:
    Quote Originally Posted by flp1969 View Post
    This identifier is the address of the entry point, it is a pointer.
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function pointer doesn't give expected output
    By vajra11 in forum C Programming
    Replies: 2
    Last Post: 12-15-2021, 05:39 AM
  2. Break doesn't work the way I expected it to
    By GiladP in forum C Programming
    Replies: 3
    Last Post: 10-11-2020, 02:11 PM
  3. Replies: 7
    Last Post: 04-23-2012, 07:52 AM
  4. Why this do-while loop doesn't work as I expected?
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-25-2002, 09:47 AM

Tags for this Thread