Live data from Hacker News

Function Pointers in C are Underrated

vickychijwani.github.com

11–20 of 33 posts

Re: Function Pointers in C are Underrated

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

Not as well known to C programmers, or not as well known to programmers who don't program C? I don't know a single C programmer that doesn't regularly use function pointers.

Re: Function Pointers in C are Underrated

#13
post #8

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

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

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

Re: Function Pointers in C are Underrated

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

Re: Function Pointers in C are Underrated

#15
post #8

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

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

No, not really. Closures... close on variables, function pointers, are just pointers to functions.

What you are saying is sort of like saying that Classes and Objects are equivalent, creating a closure creates an instance, wheras a function pointer just references the canonical code.

Also, don't undervalue anonymous local functions, which is another thing that full closures have over C99 functions.

Re: Function Pointers in C are Underrated

#16
post #8

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

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

> At the most basic level, though, the comparison remains valid, doesn't it?

Given that function pointers can't close over variables in the environment, it's a fairly tenuous comparison.

The ability to pass function around is different from the ability of functions to close over their environment. When people talk about closures and their power, they're generally talking about the latter, not the former, at least in my experience.

Re: Function Pointers in C are Underrated

#17
post #14

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.

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.

Re: Function Pointers in C are Underrated

#18
post #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…

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.

Re: Function Pointers in C are Underrated

#19
post #12

Earlier quoted context omitted.

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.

Not as well known to C programmers, or not as well known to programmers who don't program C? I don't know a single C programmer that doesn't regularly use function pointers.

Being a student who's been in the process of learning C over the past 3 years, I for one have not come across many examples of function pointers in actual use, apart from the standard library functions qsort and bsearch.

Re: Function Pointers in C are Underrated

#20
post #8

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

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

The fact that it can be used where other languages would use closures doesn't make them equal, and the language here is pretty definite, you're not using "similar", "like" or "can stand in for"… I can do a tree with an array, that doesn't make them equal.

I don't want to sound too harsh, I'm just noting that any blog post that starts out like that is a bit likely to be instantly discarded by some people. And will confuse others later on when they want to learn about closures…

Post reply on HN