

Function Pointers in C are Underrated - vickychijwani
http://vickychijwani.github.com/2012/03/22/function-pointers-in-c-are-underrated/

======
sophacles
The linux kernel has a cool linked list implementation for purposes like this,
which IMHO is much prettier. Essentially you put a struct list (or hlist)
inside of a struct you use in a list. This struct can be a simple pointer to
data, or a full struct itself. Whatever. Then the list macros and functions
just work on your data structure.

Implementation details here:
<http://lxr.linux.no/linux+v3.3/include/linux/rculist.h> (also look up
list.h).

My favorite part is wrapping your head round the container_of macro central to
this. (
[http://lxr.linux.no/linux+v3.3/tools/perf/util/include/linux...](http://lxr.linux.no/linux+v3.3/tools/perf/util/include/linux/kernel.h#L26)
)

Not that function pointers aren't neat or useful, but sometimes there are
other cool solutions too :)

------
sixbrx
The author claims you might know it as a lambda or anonymous function in other
languages, but I think function pointers come up short in comparison.

Function pointers can't capture variables from surrounding scopes and so can't
depend on runtime values in any easy or safe way.

Just try emulating the following with function pointers:

    
    
      def incrementor(n: Int) = (x:Int) => x + n
    

Sure you could add another input parameter for the C function pointer, but
then you couldn't use it anywhere an arity-1 function is required.

~~~
angersock
So, with a clever use of a macro (and assuming you use reference-counted smart
pointers or have another way of persisting variables at that time on the
stack), you can totally make a fire-and-forget closure in C++. Our solution
involved declaring inline classes with some minor stringification magic to
guarantee uniqueness.

~~~
sixbrx
Yep, there are some neat ways of emulating the capture, I didn't mean to imply
it was impossible - just beyond what workaday programmers like me are willing
to do :)

------
zerostar07
C function pointers are prime citizens for 42 years. Nobody underrates them.
'cdecl' is a fun way to learn all about them.

C++ 's inline templates beat them in performance though.

------
mangoman
This should probably be called "Function pointers in C are AWESOME". because
they are. Function Pointers are basically C's implementation for callbacks
too! There are some awesome things people can do with fp's, and i especially
like C++'s use of function pointers and function objects with respect to the
STL algorithms and the use of tr1::bind and std::not1 in the functional
header. Cool Stuff!

------
duaneb
I don't quite understand what is meant by this. Function pointers are not
"underrated"; that implies that it is lower or higher than something. Being
the only way to perform an indirect branch in C, it's not really something you
rate. It just is.

~~~
matt_yoho
I interpret the statement as being closer to "function pointers are under-
discussed".

~~~
xymostech
Indeed. While someone might talk about lambda functions very early into a
discussion of Ruby, Python, etc., pointers-to-functions in C are almost never
discussed in basic Computer Science.

------
vickychijwani
A lot of people here have pointed out that closures are not the same as
function pointers in C, and for that I thank them. I have added that note to
the blog post (it should be up on GitHub shortly), but I would also like to
say that the reason for introducing function pointers in that way was to
establish a sense of familiarity for those coming from Ruby / Python /
JavaScript, who are not well-acquainted with their use _in C_.

------
wglb
_If you know Python / Ruby / Lisp, you might know it by the name ‘lambda’, or
if you come from a JavaScript background, you might’ve used it under the name
‘anonymous function’._

This is false. Lambdas capture context; pointed-to functions i C do not.

~~~
jacquesm
I believe it is closures that capture context, lambdas are just anonymous
functions, see:

<http://en.wikipedia.org/wiki/Anonymous_function>

vs

<http://en.wikipedia.org/wiki/Closure_%28computer_science%29>

A lambda function with all its variables bound is a closure iirc.

------
mhd
Equating function pointers with closures in the second paragraph isn't a good
start…

~~~
vickychijwani
At the most basic level, though, the comparison remains valid, doesn't it?
That is all I'm trying to highlight.

~~~
sp332
No. Maybe you're thinking of anonymous functions? A closure binds a function
to its _scope_. <https://en.wikipedia.org/wiki/Closure_%28computer_science%29>

------
jacquesm
Function pointers in C are like salt. Use them sparingly and you can greatly
improve the readability and size of your code, use too many of them and you'll
end up spoiling the broth and it will become a terrible mess.

------
kbd
TLDR: You can write a comparator function and use it like with C's 'qsort'.

------
smashing
Clear and easily readable C code is underrated.

