Live data from Hacker News

Basics Of Function Pointers In C

denniskubes.com

11–20 of 61 posts

Re: Basics Of Function Pointers In C

#12
post #7

But why that whole mess with function pointers? Where do they have the key advantage compared to directly calling the function?

If you ever seen some of the C code when hacking the Sony PSP slims for the first time; particulary Davee & Bubbletune with "ChickHEN" stuff, knowing how function pointers work is required[1].

1. http://forums.exophase.com/threads/tiff-exploit-hen-informat...

Disclaimer(?): The TRFyuki here, is me. :)

Re: Basics Of Function Pointers In C

#13
Very well written article, clearly explaining what is usually a rather complex topic for new programmers to understand, bookmarked for future use.

The other resource I use for similar situations is http://c-faq.com/decl/spiral.anderson.html [EDIT]: someone beat me to it

FWIW: The explanation by Dave G in the comments isn't quite right (I couldn't reply there, comments appear closed):

> void something(); // prototype of function that takes undetermined number of arguments

> void something(void); // prototype of function that takes no arguments

> void something() { return; } // function that takes no arguments

isn't quite right as `void something(){ return; }` is still a function that takes an undetermined number of arguments, it just cannot access them; calling `something(1,2,3)` is still valid.

> void unknown(){ return ;}

> int main(void){ something(1,2,3);}

is valid.

Re: Basics Of Function Pointers In C

#14

The article is missing at least one useful thing: how to declare a typedef for a function pointer. This can be used both to avoid error-prone duplication of declarations and to simplify excessively complex declarations. Here's a simple example: http://en.wikipedia.org/wiki/Typedef#Using_typedef_with_func... Here's a more complex example: http://www.devx.com/tips/Tip/13829 Here's a tutorial on how to interpret the com…

[deleted]

Re: Basics Of Function Pointers In C

#15
post #7

But why that whole mess with function pointers? Where do they have the key advantage compared to directly calling the function?

Let's look at the signature of qsort() from libc: void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); qsort is a function inside of libc.so. It's already been compiled. It doesn't know about the "compar" pointer you're going to pass it. That function might not even exist yet. qsort doesn't know if you're sorting integers, sorting strings, sorting struct foo which is still bei…

thanks, that helped me out <3

Re: Basics Of Function Pointers In C

#16

Understanding function pointers in C unlocks the ability to write clean, object-oriented code with inheritance (kinda, sorta, shhhh). With great power, etc. etc.

I committed what I now realize were true crimes against software engineering when I first learned of function pointers.

Re: Basics Of Function Pointers In C

#17

The article is missing at least one useful thing: how to declare a typedef for a function pointer. This can be used both to avoid error-prone duplication of declarations and to simplify excessively complex declarations. Here's a simple example: http://en.wikipedia.org/wiki/Typedef#Using_typedef_with_func... Here's a more complex example: http://www.devx.com/tips/Tip/13829 Here's a tutorial on how to interpret the com…

The most useful education I got on reading function pointers is the spiral rule:

http://c-faq.com/decl/spiral.anderson.html

(I accidentally deleted my reply)

Re: Basics Of Function Pointers In C

#18
post #7

But why that whole mess with function pointers? Where do they have the key advantage compared to directly calling the function?

One great example of function pointers is in C++ when implementing inheritance. Say you have an array of Polygons, and each one has an Area() method. It turns out that some of them are Triangles, some are Squares, and some are Hexagons. polygon[i]->Area() calls a different function for each of these, and this is implemented by storing function pointers in "vtables" for each of the Polygon objects. In C++ this happens behind the scenes (you don't program this directly), but in C (or C++ for that matter if you needed to), you can get a very similar effect by storing function pointers in structs. The linux kernel makes extensive use of this programming style in driver code.

Re: Basics Of Function Pointers In C

#19

The article is missing at least one useful thing: how to declare a typedef for a function pointer. This can be used both to avoid error-prone duplication of declarations and to simplify excessively complex declarations. Here's a simple example: http://en.wikipedia.org/wiki/Typedef#Using_typedef_with_func... Here's a more complex example: http://www.devx.com/tips/Tip/13829 Here's a tutorial on how to interpret the com…

It's also good to note that these links for further reading are applicable for blocks in Objective-C, which use the same syntax for declaration, only replacing the asterisk '*' with a caret '^'.

Re: Basics Of Function Pointers In C

#20

Understanding function pointers in C unlocks the ability to write clean, object-oriented code with inheritance (kinda, sorta, shhhh). With great power, etc. etc.

I committed what I now realize were true crimes against software engineering when I first learned of function pointers.

They can't be any worse than abusing the C preprocessor to implement templates for C code (a sin I committed in college).
Post reply on HN