Reusing function signature for both a function pointer and function.

Discussion in 'C Programming' started by daniel rich, Nov 13, 2012.

  1. daniel rich

    daniel rich Guest

    Hello All,

    I have been wrestling with a problem, and my others searches have failed so I wanted to ask here.

    Thanks in advance for any thoughts/help.

    I currently have some code that looks something like this.

    someHeader.h
    "
    typedef void (* proj_getNameFuncPtr) (char* nameBuffer );

    void proj_getName( char* nameBuffer );
    "
    someFile.c
    "
    void proj_getName(char* nameBuffer )
    {
    //implement
    }
    "


    Ideally I was hoping to do something like the following instead, so that I can reduce the code duplication. In the actual project there are quite a few of these and
    maintaining the signature in both the typedef'd func pointer and the function dec'l is a pain.

    newHeader.h
    "
    typedef void proj_getNameFunc( char* nameBuffer );

    typedef proj_getNameFunc* proj_getNameFuncPtr;

    proj_getNameFunc proj_getName;
    "
    newFile.c
    "
    void proj_getName(char* nameBuffer )
    {
    //implement
    }
    "

    The compiler I am using(msvc71) chokes on "typedef proj_getNameFunc* proj_getNameFuncPtr;" and complains.
    "error: expected '=', ',', ';', 'asm' or '__attribute__' before * token"


    I want to keep the func pointers around since we do end up using quite a few of them in function signatures. I may be doing things the totally wrong way but would appreciate any correction/help that could be provided.

    --Daniel Rich
     
    daniel rich, Nov 13, 2012
    #1
    1. Advertising

  2. daniel rich

    Ian Collins Guest

    On 11/14/12 12:15, daniel rich wrote:

    ** please wrap your lines! *

    > Hello All,
    >
    > I have been wrestling with a problem, and my others searches have
    > failed so I wanted to ask here.
    >
    > Thanks in advance for any thoughts/help.
    >
    > I currently have some code that looks something like this.
    >
    > someHeader.h " typedef void (* proj_getNameFuncPtr) (char* nameBuffer
    > );
    >
    > void proj_getName( char* nameBuffer ); " someFile.c " void
    > proj_getName(char* nameBuffer ) { //implement } "
    >
    >
    > Ideally I was hoping to do something like the following instead, so
    > that I can reduce the code duplication. In the actual project there
    > are quite a few of these and maintaining the signature in both the
    > typedef'd func pointer and the function dec'l is a pain.


    Firstly, why do you have to maintain both?

    What do you use the function pointer type for and where is the duplication?

    > newHeader.h " typedef void proj_getNameFunc( char* nameBuffer );
    >
    > typedef proj_getNameFunc* proj_getNameFuncPtr;
    >
    > proj_getNameFunc proj_getName; " newFile.c " void proj_getName(char*
    > nameBuffer ) { //implement } "
    >
    > The compiler I am using(msvc71) chokes on "typedef proj_getNameFunc*
    > proj_getNameFuncPtr;" and complains. "error: expected '=', ',', ';',
    > 'asm' or '__attribute__' before * token"


    If the error is caused by the code you posted, the compiler is broken...

    > I want to keep the func pointers around since we do end up using
    > quite a few of them in function signatures. I may be doing things the
    > totally wrong way but would appreciate any correction/help that could
    > be provided.


    How? Some use cases might make your problem clearer.

    --
    Ian Collins
     
    Ian Collins, Nov 13, 2012
    #2
    1. Advertising

  3. On Tue, 13 Nov 2012 15:15:34 -0800 (PST), daniel rich
    <> wrote:

    snip

    >Ideally I was hoping to do something like the following instead, so that I can reduce the code duplication. In the actual project there are quite a few of these and
    >maintaining the signature in both the typedef'd func pointer and the function dec'l is a pain.
    >
    >newHeader.h
    >"
    >typedef void proj_getNameFunc( char* nameBuffer );
    >
    >typedef proj_getNameFunc* proj_getNameFuncPtr;
    >
    >proj_getNameFunc proj_getName;
    >"
    >newFile.c
    >"
    >void proj_getName(char* nameBuffer )
    >{
    >//implement
    >}
    >"
    >
    >The compiler I am using(msvc71) chokes on "typedef proj_getNameFunc* proj_getNameFuncPtr;" and complains.
    >"error: expected '=', ',', ';', 'asm' or '__attribute__' before * token"
    >
    >
    >I want to keep the func pointers around since we do end up using quite a few of them in function signatures. I may be doing things the totally wrong way but would appreciate any correction/help that could be provided.


    My compiler which is at the C90 level accepts the two typedefs and the
    prototype. Are you sure what you posted is exactly what you compiled?

    Once you get it to work, you still have the problem of "synching" the
    parameter list in the function definition with the one in the typedef.
    Have you considered using a #define.

    #define proj_getNameFuncParm type1 name1, type2 name 2
    typedef void proj_getNameFunc( proj_getNameFuncParm);
    typedef proj_getNameFunc* proj_getNameFuncPtr;
    proj_getNameFunc proj_getName;

    void proj_getName(proj_getNameFuncParm)
    {
    //implement
    }

    --
    Remove del for email
     
    Barry Schwarz, Nov 14, 2012
    #3
  4. Re: Reusing function signature for both a function pointer andfunction.

    On 2012-11-14, Robert Wessel <> wrote:
    > But *what* compiler are you using? MSVC 7.1 is 20 years old! The
    > oldest version I could easily run (v8.0 - circa 1993) seems just fine.


    20 years? Circa 1993?

    "Visual C++ .NET 2003 (known also as Visual C++ 7.1), which included MFC
    7.1, was released in 2003 --"

    "Visual C++ 2005 (known also as Visual C++ 8.0), which included MFC 8.0,
    was released in November 2005 --"

    These are the compilers that I believe the names "MSVC 7.1" and "MSVC
    8.0" are most often used to refer to. Versions before the "Visual C++"
    brand ("Microsoft C/C++"), as far as I know, don't go beyond 7.0, since
    Visual C++ 1.0 was the follow-up to that. Disclaimer: most of the above
    is second-hand knowledge; though I did use "MSVC 6" aka "Visual C++ 6.0"
    quite a lot, and it was the new thing in 1998.

    (Admittedly, it will soon be a decade since 2003...)

    --
    Heikki Kallasjoki
     
    Heikki Kallasjoki, Nov 14, 2012
    #4
  5. daniel rich

    Noob Guest

    Noob, Nov 14, 2012
    #5
  6. daniel rich

    daniel rich Guest

    I am mortified to admit that after all of the comments that it should
    work I realized that I did in fact have a typo in the original code.
    It now works as intended and I may also use the #define for arguments as
    suggested above.

    Sorry for the wasted time and thanks for the suggestions.

    The compiler is somewhat old but mandated for the project, and the funcPtr
    typedef are needed b/c they are used through the codebase, and I only
    have control over a small portion of said codebase.
     
    daniel rich, Nov 14, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JJBW
    Replies:
    1
    Views:
    10,331
    Joerg Jooss
    Apr 24, 2004
  2. sqweek
    Replies:
    2
    Views:
    415
    Charles Rapp
    Jan 13, 2006
  3. David Ching
    Replies:
    0
    Views:
    392
    David Ching
    Aug 29, 2007
  4. Gustavo Narea
    Replies:
    14
    Views:
    876
    Gustavo Narea
    Feb 16, 2009
  5. David Ching

    Reusing same DetailsView for both Inserting and Editing

    David Ching, Aug 30, 2007, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    178
    David Ching
    Aug 30, 2007
Loading...

Share This Page