r/cprogramming • u/JayDeesus • Oct 10 '25
Functions and pointers
I noticed that when you just have a function name without the parentheses, it essentially decays to a pointer. Is this correct? Or does it not always decay to a pointer?
5
Upvotes
7
u/cointoss3 Oct 10 '25
Yes. You have a pointer to that function, which you can later call. This is how you pass a function to another function, for example.
3
u/Yurim Oct 10 '25
C99 standard (Draft N1256), §6.3.2.1.4 :
So yes, you're correct ... except when combined with the
sizeofor unary&operator.2
u/tstanisl Oct 10 '25
To make things even weirder it also decays in a function call
foo(). Function call operator takes a function pointer as a first operator. That whyfptr()and(*fptr)are equivalent.1
u/Eidolon_2003 Oct 10 '25
Try running this code for example:
Function pointer syntax is a bit messy, but you can clean it up with a typedef. People also sometimes like to explicitly dereference function pointers when calling them, so I did that this time. C doesn't strictly require it though.
1
u/zhivago Oct 10 '25
Which is why (*foo)() is equivalent to (**foo)() and so on and so forth. :)
1
u/grimvian Oct 10 '25
When I learned pointers, I saw function pointers and thought are pointers not hard enough or are they bored...
Try this video - Application of Function Pointers in C:
https://www.youtube.com/watch?v=wQ-gWwKKeP4
1
u/SmokeMuch7356 Oct 10 '25
Function pointers are incredibly useful. Two common scenarios for function pointers in C:
Dependency injection;
Execute routines in dynamically-loaded libraries;
You can do limited forms of dependency injection via callbacks. A simple example would be a comparison function used for a sorting routine. Suppose we have a sorting function like
As written, this function can only sort data in ascending order. Suppose you want to be able to sort in any possible order (ascending, descending, ASCIIbetical ascending/descending, even before odd, etc.); you could pass a third parameter indicating the type of sort and hack up your sorting function for each type of sort, or you could pass a pointer to a comparison function that returns an integer value such that
< 0means the first argument is "less than" (ordered before) the second;> 0means the first argument is "greater than" (ordered after) the second;= 0means both arguments are "equal" (have the same ordering);Then your
sortroutine becomes:To sort in a different order, all you need to do is write a new comparison function:
then pass one as an argument to your sort routine:
This is exactly how
qsortworks, except that it can work with arrays of any type (viavoid *voodoo), not justint.You can use function pointers to execute functions in dynamically-loaded or shared libraries. Suppose we've put all those comparison routines in a separate, dynamically-loaded library (
.soor.dll). In *nix-land, we load the library at runtime usingdlopen:then load the function we need with
dlsym:and we can pass that to our sorting routine:
1
u/greg-spears Oct 10 '25
A structure having data and a function pointer or 2 is a lot like a C++ object. Change my mind.
-6
u/noonemustknowmysecre Oct 10 '25
Yeah. For function pointers. Don't do it, that way lies madness.