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…
Function Pointers in C are Underrated
21–30 of 33 posts
Re: Function Pointers in C are Underrated
#22If 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.
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.
Re: Function Pointers in C are Underrated
#23Earlier quoted context omitted.
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
#24I 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
#25Earlier quoted context omitted.
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.
If you want a good example of where they're useful, write the interpreter loop for a simple 3-byte wordsize VM.
To expand on that, make an array of function pointers corresponding to your opcodes. Have the eval() function for a bytecode instruction use the first byte of the bytecode as the array index, and then call the function pointer at that position with the other two bytes as arguments. It's a pretty solid use case.
Also, you're not strictly incorrect--the function pointer does "close over" the execution environment of the system at that time, though that's more a feature of C and globals and pointer arithmetic than anything else. ;)
Re: Function Pointers in C are Underrated
#26Re: Function Pointers in C are Underrated
#27Re: Function Pointers in C are Underrated
#28Earlier quoted context omitted.
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.
No harm. :) If you want a good example of where they're useful, write the interpreter loop for a simple 3-byte wordsize VM. To expand on that, make an array of function pointers corresponding to your opcodes. Have the eval() function for a bytecode instruction use the first byte of the bytecode as the array index, and then call the function pointer at that position with the other two bytes as arguments. It's a pretty…
Re: Function Pointers in C are Underrated
#29Earlier quoted context omitted.
No harm. :) If you want a good example of where they're useful, write the interpreter loop for a simple 3-byte wordsize VM. To expand on that, make an array of function pointers corresponding to your opcodes. Have the eval() function for a bytecode instruction use the first byte of the bytecode as the array index, and then call the function pointer at that position with the other two bytes as arguments. It's a pretty…
I'm not sure that closing over only global really counts...