Thread: function declaration in Main function

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    function declaration in Main function

    Hello,
    i am browsing sources for Torque resource manager and there is this particular line in main function that declares function pointer of some kind:

    int schedule A_((int com, int connector));

    I know what it does, when the function is called it calls function implemented in one of its libraries libfoo.la However, I haven't seen this kind of syntax (with A_) before. Can anybody give me hint's or reference where i can search my answer? I would really like to know how exactly this works.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    That's not a function pointer, it's a function declaration. The A_ is a macro defined somewhere in the source. It looks to be used to create code that builds with both K&R compilers and modern compilers.

    Prototypes are a somewhat new feature in C, and so really old compilers don't support them. However, it'd be stupid to not use prototypes in new code because they're really useful. Thus people who, for some reason, care about older (roughly pre-1990) compilers, use a trick like the following to do this:
    Code:
    #if __STDC__ == 1
    #define PROTO(p) p
    #else
    #define PROTO(p) ()
    #endif
    
    int f PROTO((const char *));
    
    /* Under older compilers (__STDC__ is not 1), you get: */
    int f ();
    
    /* Under modern compilers (__STDC__ is 1), you get: */
    int f (const char *);
    The second version is clearly superior, but if your compiler doesn't support it, you've got to use the inferior version.

    To see exactly what the macro in your sources looks like, grep for A_ (it's probably in a header somewhere).

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    2
    Quote Originally Posted by cas View Post
    That's not a function pointer, it's a function declaration. The A_ is a macro defined somewhere in the source. It looks to be used to create code that builds with both K&R compilers and modern compilers.

    Prototypes are a somewhat new feature in C, and so really old compilers don't support them. However, it'd be stupid to not use prototypes in new code because they're really useful. Thus people who, for some reason, care about older (roughly pre-1990) compilers, use a trick like the following to do this:
    Code:
    #if __STDC__ == 1
    #define PROTO(p) p
    #else
    #define PROTO(p) ()
    #endif
    
    int f PROTO((const char *));
    
    /* Under older compilers (__STDC__ is not 1), you get: */
    int f ();
    
    /* Under modern compilers (__STDC__ is 1), you get: */
    int f (const char *);
    The second version is clearly superior, but if your compiler doesn't support it, you've got to use the inferior version.

    To see exactly what the macro in your sources looks like, grep for A_ (it's probably in a header somewhere).
    Thank you very much for comprehensive answer. I have located the macro definition and it's very clear now

    #ifdef __STDC__
    /*
    * The following macro definations take affect when compiling under ansi C
    *
    * The A_ macro is provided for function prototype declarations. It allows
    * ANSI C prototypes to be complied under K&R C
    */

    #define A_(x) x

    #else

    /* The following macro definations take affect when compiling under K&R C */

    #define A_(x) ()
    #define const
    #define volatile

    #endif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. Main Declaration error
    By starkhorn in forum C++ Programming
    Replies: 11
    Last Post: 06-22-2005, 09:04 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

Tags for this Thread