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

  • Thread starter Jens Schweikhardt
  • Start date
J

Jens Schweikhardt

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
 
B

Ben Bacarisse

Jens Schweikhardt said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top