Thread: Be confused with one function prototype

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

    Unhappy Be confused with one function prototype

    Here the function prototype manual goes,
    Xlib Programming Manual: XSetErrorHandler

    Code:
    int (*XSetErrorHandler(handler))()
          int (*handler)(Display *, XErrorEvent *)
    I'm confused with the first line
    Code:
    int (*XSetErrorHandler(handler))()
    Most people use the function like that
    Code:
    int (*handler)(Display *, XErrorEvent *) = NULL;
    int old(Display *d, XErrorEvent *e){...}
    handler = XSetErrorHandler(old);
    XSetErrorHandler(handler);
    Others direct invoke the function
    Code:
    XSetErrorHandler(old);
    I have no idea how the hell XSetErrorHandler works.

    As I understood for XSetErrorHandler()
    At first it in the bracket returns one function which returned pf integer value,and then invoke the function.
    But it conflicts with
    Code:
    handler = XSetErrorHandler(old);
    which obvisouly return function pointer.


    what's the theory of the function?
    how hell does function works?



    I'm looking forware to the explanation.

    Ching.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2
    okay i get it from someone's help.
    typedef int (*HANDLER)(Display*, XErrorEvent*);
    HANDLER XSetErrorHandler( HANDLER handler );

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's just a function which takes a pointer to a function as a parameter, and returns a pointer to a function (the previous handler).

    Sometimes you want to temporarily use your own handler
    Code:
    old = XSetErrorHandler(handler);
    /// code
    XSetErrorHandler(old);
    Other times, you want a permanent replacement, so you ignore the return result.
    Code:
    XSetErrorHandler(handler);
    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.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by chings View Post
    Here the function prototype manual goes,
    Xlib Programming Manual: XSetErrorHandler

    Code:
    int (*XSetErrorHandler(handler))()
          int (*handler)(Display *, XErrorEvent *)
    First line is the prototype for XSetErrorHandler() while the next line clarifies that the single argument to XSetErrorHandler() ie handler is a pointer to a function that returns an int.
    Quote Originally Posted by chings View Post
    I'm confused with the first line
    Code:
    int (*XSetErrorHandler(handler))()
    It means that XSetErrorHandler is a function that takes a single argument and returns a pointer to a function that returns an int.
    Quote Originally Posted by chings View Post
    Most people use the function like that
    Code:
    int (*handler)(Display *, XErrorEvent *) = NULL;
    int old(Display *d, XErrorEvent *e){...}
    handler = XSetErrorHandler(old);
    XSetErrorHandler(handler);
    That is correct since the return value of XSetErrorHandler() is being assigned to handler.
    Quote Originally Posted by chings View Post
    Others direct invoke the function
    Code:
    XSetErrorHandler(old);
    I have no idea how the hell XSetErrorHandler works.
    Also correct except the return value of XSetErrorHandler() is lost.
    Quote Originally Posted by chings View Post
    As I understood for XSetErrorHandler()
    At first it in the bracket returns one function which returned pf integer value,and then invoke the function.
    But it conflicts with
    Code:
    handler = XSetErrorHandler(old);
    which obvisouly return function pointer.


    what's the theory of the function?
    how hell does function works?



    I'm looking forware to the explanation.

    Ching.
    The above assignment statement is correct because XSetErrorHandler() takes old as its argument - a function that takes two arguments and returns an int.
    old is the name of a function and used by itself becomes a pointer to that function which is exactly the type of the argument that XSetErrorHandler() expects.
    XSetErrorHandler() returns a pointer to a function returning int, which agrees with the declaration of handler as it is a pointer to a function that returns an int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Replies: 13
    Last Post: 08-24-2006, 12:22 AM