Like Tree3Likes
  • 1 Post By laserlight
  • 1 Post By grumpy
  • 1 Post By grumpy

initialization of function pointer

This is a discussion on initialization of function pointer within the C Programming forums, part of the General Programming Boards category; Hello I would like to initialize an arry containing function pointers with adresses of functions that have a variable number ...

  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
    ZuK
    ZuK is offline
    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
    21,648
    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.
    King Mir likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    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.
    King Mir likes this.
    Right 98% of the time, and don't care about the other 3%.

  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
    21,648
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    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.
    Salem likes this.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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-22-2012, 11:05 PM
  2. Pointer Initialization
    By johan.g1 in forum C Programming
    Replies: 6
    Last Post: 11-14-2012, 08:08 PM
  3. Struct pointer initialization
    By pe4enka in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2010, 04:16 AM
  4. Pointer Initialization Within A Struct
    By SMurf in forum C Programming
    Replies: 15
    Last Post: 02-09-2009, 10:27 AM
  5. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 11:42 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21