What the point of "typedef int func(void);"?

Discussion in 'C Programming' started by Jens Schweikhardt, Apr 1, 2012.

  1. hello, world\n

    looking at FreeBSD's <sys/signal.h> I came across this fragment:

    #define SIG_ERR ((__sighandler_t *)-1)
    ...
    typedef void __sighandler_t(int);

    and I started to wonder what the typedef meant. It's not typedefing
    an alias for a function pointer, that would look like

    typedef void (*func_ptr)(int);

    so it apparently is an alias for a function. Compiled with gcc the
    sizeof(__sighandler_t) is 1, while the sizeof(func_ptr) is 8. I can
    declare an object of type __sighandler_t but I can't assign to it,
    because functions can't be assigned to, only function pointers can.
    It appears the only use of such a typedef-name is in casts and
    declarators. Is that correct?


    Regards,

    Jens
    --
    Jens Schweikhardt http://www.schweikhardt.net/
    SIGSIG -- signature too long (core dumped)
    Jens Schweikhardt, Apr 1, 2012
    #1
    1. Advertising

  2. Jens Schweikhardt <> writes:

    > hello, world\n
    >
    > looking at FreeBSD's <sys/signal.h> I came across this fragment:
    >
    > #define SIG_ERR ((__sighandler_t *)-1)
    > ...
    > typedef void __sighandler_t(int);
    >
    > and I started to wonder what the typedef meant. It's not typedefing
    > an alias for a function pointer, that would look like
    >
    > typedef void (*func_ptr)(int);
    >
    > so it apparently is an alias for a function.


    For a function *type*, yes.

    > Compiled with gcc the
    > sizeof(__sighandler_t) is 1,


    GCC should issue a diagnostic since this is constraint violation, but' it
    can then goon to give whatever size it likes.

    > while the sizeof(func_ptr) is 8. I can
    > declare an object of type __sighandler_t but I can't assign to it,
    > because functions can't be assigned to, only function pointers can.
    > It appears the only use of such a typedef-name is in casts and
    > declarators. Is that correct?


    Not quite. You can use it to declare (but not define) functions as
    well. I've done the following more than once:

    typedef value binary_op(value, value);

    static binary_op add, sub, mul, div, rem, mod;

    static binary_op *operator_table[] = { add, sub, mul, div, rem, mod };

    static value add(value x, value y) { return x + y; }
    /* etc */

    Neither of these uses is, I think, in a declarator. I am being a little
    fussy about terminology here. Maybe this use is covered by what you
    meant.

    --
    Ben.
    Ben Bacarisse, Apr 1, 2012
    #2
    1. Advertising

  3. Jens Schweikhardt

    feedscrn Guest

    On Apr 1, 5:35 pm, Ben Bacarisse <> wrote:
    > .............


    Cool coding. Thumbs up.
    feedscrn, Apr 5, 2012
    #3
    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. Gopi

    point to point protocol

    Gopi, Jul 13, 2004, in forum: VHDL
    Replies:
    1
    Views:
    469
    Mike Treseler
    Jul 13, 2004
  2. H aka N
    Replies:
    15
    Views:
    15,643
    Ben Jones
    Mar 2, 2006
  3. Motaz Saad
    Replies:
    7
    Views:
    6,478
  4. USCode
    Replies:
    7
    Views:
    425
    jeffc
    Jul 9, 2004
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,321
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page