Sunday, October 31, 2010

Interpreting C function pointer syntax

How to interpret C function pointer syntax which borders on the arcane. e.g. "typedef void (*sighandler_t)(int);" ?

This is the general syntax for a function pointer is always
return_type (* pointer_name) ( variable1_type variable1_name , variable2_type variable2_name,....)
See more explanation and example at:
http://www.cprogrammingreference.com/Tutorials/Basic_Tutorials/Pointers.php#10


In my first example with a typedef, that is defining a function pointer as a type. Ignore the typedef and it is a function "sighandler_t", returns void and accepts one argument of type int. The (* ... ) groups the pointer asterisk with the typedef's name and indicates that the typdef is for a function pointer. e.g. "void sighandler_t(int);".

See more discussion at Stack Overflow: Reading function pointer syntax

No comments:

Post a Comment