top | item 3741417

Function Pointers in C are Underrated

19 points| vickychijwani | 14 years ago |vickychijwani.github.com | reply

32 comments

order
[+] sophacles|14 years ago|reply
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... )

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

[+] sixbrx|14 years ago|reply
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|14 years ago|reply
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.
[+] jasonlotito|14 years ago|reply
sigh

FTA: The point was not to go into the nuances of closures and lambdas, but only to highlight the basic similarity between function pointers / lambdas / anonymous functions.

[+] zerostar07|14 years ago|reply
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|14 years ago|reply
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|14 years ago|reply
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|14 years ago|reply
I interpret the statement as being closer to "function pointers are under-discussed".
[+] vickychijwani|14 years ago|reply
Through the title, I'm just trying to point out that function pointers are not as well-known in C as in other languages. Programmers should recognize when and how to use them effectively.
[+] vickychijwani|14 years ago|reply
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|14 years ago|reply
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.

[+] jasonlotito|14 years ago|reply
FTA: The point was not to go into the nuances of closures and lambdas, but only to highlight the basic similarity between function pointers / lambdas / anonymous functions.
[+] mhd|14 years ago|reply
Equating function pointers with closures in the second paragraph isn't a good start…
[+] vickychijwani|14 years ago|reply
At the most basic level, though, the comparison remains valid, doesn't it? That is all I'm trying to highlight.
[+] jacquesm|14 years ago|reply
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|14 years ago|reply
TLDR: You can write a comparator function and use it like with C's 'qsort'.
[+] smashing|14 years ago|reply
Clear and easily readable C code is underrated.