Live data from Hacker News

Function Pointers in C are Underrated

vickychijwani.github.com

1–10 of 33 posts

Re: Function Pointers in C are Underrated

#2
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.

Re: Function Pointers in C are Underrated

#5
post #2

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.

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

Re: Function Pointers in C are Underrated

#6
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!

Re: Function Pointers in C are Underrated

#7
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 :)

Re: Function Pointers in C are Underrated

#9
post #2

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.

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.

Re: Function Pointers in C are Underrated

#10
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.
Post reply on HN