typedef - function pointers

This is a discussion on typedef - function pointers within the C++ Programming forums, part of the General Programming Boards category; Hello, I am rather puzzled by the following (from Chapter 7 of Stroustrup): Code: typdef void (*SIG_TYP)(int); typdef void (*SIG_ARG_TYP)(int); ...

  1. #1
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99

    typedef - function pointers

    Hello,

    I am rather puzzled by the following (from Chapter 7 of Stroustrup):

    Code:
    typdef void (*SIG_TYP)(int);
    typdef void (*SIG_ARG_TYP)(int);
    SIG_TYP signal(int, SIG_ARG_TYP);
    What bothers me is the parameter list in the third line. If SIG_TYP is a pointer to a function that takes an int as its argument, why is the parameter list needed and what is signal actually being declared as?

    Any help would be most appreciated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    signal is a function. It returns SIG_TYP, that is, it returns a pointer-to-function-taking-int-returning-void. It has two parameters; the first is of type int, the second of type SIG_ARG_TYP, that is, a pointer-to-function-taking-int-returning-void. (Yes, SIG_TYP and SIG_ARG_TYP are the same type, but they have different uses, which is what is being distinguished here.)

  3. #3
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    Thanks. Quite obvious really, once you know the answer.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    It's also possible to do it like this (which I like and use):
    Code:
    typedef void (SIG_TYP)(int);
    typedef void (SIG_ARG_TYP)(int);
    SIG_TYP* signal(int, SIG_ARG_TYP*);
    Because it makes it more obvious they are pointers.
    And for the love of the gods, don't follow this example in that you omit the names of the parameters in the prototypes. Leave the names in.
    Last edited by Elysia; 10-15-2008 at 06:32 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    5,883
    Also, to keep your compilers sane, note that examples in this thread are missing an 'e' from typedef.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Ah, not mine though
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes, well, I see that your post has been edited.

    And the other alternative, of course, is to not use typedefs at all.
    http://www.opengroup.org/onlinepubs/...ns/signal.html
    I wouldn't recommend it where function pointers are concerned, though.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    To DL1:

    Would you prefer to have code written like this?

    Code:
    void (*signal (int , void (*)(int))) (int);
    Which certainly says the same thing, but it does so in a way that isn't necessarily as easy to follow.
    Last edited by master5001; 10-15-2008 at 04:55 PM.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by master5001 View Post
    To DL1:

    Would you prefer to have code written like this?

    Code:
    void (*)(int) signal(int, void (*)(int));
    Which certainly says the same thing, but it does so in a way that isn't necessarily as easy to follow.
    That's invalid.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Good point. Not to keep anyone in suspense, the valid prototype is:
    Code:
    void (*signal (int , void (*)(int))) (int);
    Looks backwards, since the parameters to the function itself are on the inside, and the parameters of the returned function are on the outside.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah yes. I knew there was a reason why I never returned pointers to functions without typedefing. Sorry OP.

  12. #12
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    master5001,

    I think I would have, but only because I hadn't worked out how typedefs were used. That prototype is pretty horric though!

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    That it certainly is, as is the definition!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Replies: 4
    Last Post: 11-23-2003, 06:15 AM

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