Thread: initialization of function pointer

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    2

    initialization of function pointer

    Hello

    I would like to initialize an arry containing function pointers with adresses of functions that have a variable number of arguments.

    Below the code that works in principle. I would however get rid of the warning message during compilation pointing to the initialzation of the funtion pointers in the array. How do I need to cast the pointers to the functions ?


    Code:
    gcc func_ptr_init.c
    func_ptr_init.c:28: warning: initialization from incompatible pointer type
    func_ptr_init.c:32: warning: initialization from incompatible pointer type

    Code:
    #include<stdio.h>
    
    unsigned char func1_ptr(unsigned int* if_desc,  int* result_code)
    {
            *if_desc = 1;
            *result_code = 1;
            return(0);
    }
    
    unsigned char func2_ptr(unsigned int* if_desc,  int* result_code)
    {
            *if_desc = 2;
            *result_code = 2;
            return(0);
    }
    struct _server_command_table_t
    {
            char*           command_string;
            unsigned char   (*callback_func)(unsigned int* if_desc, ...);
    };
    
    
    struct _server_command_table_t table[] =
    {
            {
                    "String1",
                    &func1_ptr
            },
            {
                    "String2",
                    &func2_ptr
            }
    };
    
    int main(int argc,char **argv)
    {
            int interface;
            int result;
    
            printf("%s\n",table[0].command_string);
            printf("%d\n",table[0].callback_func(&interface,&result));
            printf("interface=%d, result=%d\n",interface,result);
    
            return(0);
    };
    Thanks
    Tilman

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    use a typedef

    Code:
    #include<stdio.h>
     
    unsigned char func1_ptr(unsigned int* if_desc,  int* result_code)
    {
            *if_desc = 1;
            *result_code = 1;
            return(0);
    }
     
    unsigned char func2_ptr(unsigned int* if_desc,  int* result_code)
    {
            *if_desc = 2;
            *result_code = 2;
            return(0);
    }
    
    typedef unsigned char   (*callback_func)(unsigned int* if_desc, ...);
    
    struct _server_command_table_t
    {
            char*           command_string;
            callback_func   callback;
    };
     
    
     
    struct _server_command_table_t table[] =
    {
            {
                    "String1",
                    (callback_func)&func1_ptr
            },
            {
                    "String2",
                    (callback_func)&func2_ptr
            }
    };
     
    int main(int argc,char **argv)
    {
            int interface;
            int result;
     
            printf("%s\n",table[1].command_string);
            printf("%d\n",table[1].callback(&interface,&result,23));
            printf("interface=%d, result=%d\n",interface,result);
     
            return(0);
    };
    Kurt

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think the appropriate solution is to change the signatures of func1_ptr and func2_ptr so that they are functions that accept a variable number of arguments, i.e., change their type to match the function pointer member of struct _server_command_table_t.

    Casting would work if you are certain that the functions are compatible with functions that accept a variable number of arguments, but frankly, I'm not sure if they are.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    Casting would work if you are certain that the functions are compatible with functions that accept a variable number of arguments, but frankly, I'm not sure if they are.
    In older versions of C, predating C89, there was such compatibility. However, that was no longer guaranteed as of the first C standard, because of tightening up of various rules related to type safety (and also explicitly allowing compiler implementers more flexibility in how arguments are passed to functions).

    Among other things, that is why modern compilers refuse to allow implicit conversion between pointers to functions if there is any difference in signature, as in the example here.

    Using an explicit type conversion between different pointers to function (aka typecast) will bludgeon the compiler into submission but the end result - when the function is eventually called - will be undefined behaviour.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    2
    Hello

    thanks for you suggestions.
    typedef works, of course, and is probably the most readeable solution. I will use it in my program.
    I still wonder however, how I would cast it ? I am just getting syntax errors..

    Thanks
    Tilman

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tilman
    typedef works, of course, and is probably the most readeable solution. I will use it in my program.
    I still wonder however, how I would cast it ? I am just getting syntax errors..
    grumpy noted in post #3 that the functions that you have in mind are not compatible with functions that accept a variable number of arguments. As such, my concern in post #2 applies. Therefore, you should not use it in your program, i.e., ignore ZuK's suggestion.

    Rather, do as I suggested: change the signatures of func1_ptr and func2_ptr so that they are functions that accept a variable number of arguments. If necessary, write wrapper functions that themselves take a variable number of arguments but which call the functions that you have in mind.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by tilman View Post
    I still wonder however, how I would cast it ?
    I have to say, you are brave.

    Explicit type conversions (aka typecasts) should be a last resort to problem solving. In contrast, you are reaching for typecasts automatically to solve little concerns like syntax errors. In C programming circles, doing that is considered to be either very brave or very foolish. I have given you the benefit of the doubt in describing you as brave.

    If you have to use typecasts to stop a compiler complaining about syntax errors, that is a positive sign your design and code is broken. Even if you successfully build your program after introducing typecasts, odds are it won't work.

    Given that your func1_ptr and func2_ptr have identical signatures, it would be appropriate that your callback function have the same signature. That way, you can solve your problem without using typecasts at all.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The unadorned pointer cast looks like this - it's not something you want to be copy/pasting a lot of times.
    (unsigned char(*)(unsigned int* if_desc, ...))&func2_ptr


    > printf("%d\n",table[1].callback(&interface,&result,23));
    Take heed of the other advice, and consider a different approach.
    No function posted deals with the 3rd parameter passed here.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    @tilman

    Why you didn't write

    Code:
    struct _server_command_table_t
    {
            char*           command_string;
            unsigned char   (*callback_func)(unsigned int* if_desc, int* result_code);
    };
    ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does static initialization of pointer make it null pointer
    By Saurabh Mehta in forum C Programming
    Replies: 1
    Last Post: 11-23-2012, 12:05 AM
  2. Pointer Initialization
    By johan.g1 in forum C Programming
    Replies: 6
    Last Post: 11-14-2012, 09:08 PM
  3. Struct pointer initialization
    By pe4enka in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2010, 05:16 AM
  4. Pointer Initialization Within A Struct
    By SMurf in forum C Programming
    Replies: 15
    Last Post: 02-09-2009, 11:27 AM
  5. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM

Tags for this Thread