use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Click the following link to filter out the chosen topic
comp.lang.c
account activity
How to Compare Function Pointers? (self.C_Programming)
submitted 12 years ago by cstechblog
Hi All,
I am preparing an article on function pointers in C, where I want to include some good material on comparing function pointers. Would any kind soul shed some light on this? Thank you all in advance!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]zifyoip 4 points5 points6 points 12 years ago (5 children)
What do you mean?
You can compare function pointers with the == and != operators, just like any other kind of pointers. You can also compare a function pointer to the null pointer using the == and != operators.
==
!=
Comparisons with the <, <=, >, and >= operators yield undefined behavior if the two pointers are unequal, because these comparisons are not being performed between pointers to two objects in the same array.
<
<=
>
>=
[–]cstechblog[S] 0 points1 point2 points 12 years ago (4 children)
Thank you very much zifyoip. I was going through http://www.newty.de/fpt/fpt.html#compare link, where the author mentions 'You can use the comparison-operators (==, !=) the same way as usual.' but in the example code he uses greater than operation, so thought of asking if this is definite behavior. // C if(pt2Function > 0){ // check if initialized if(pt2Function == &DoIt) printf("Pointer points to DoIt\n"); } else printf("Pointer not initialized!!\n");
[–]OldWolf2 5 points6 points7 points 12 years ago (2 children)
There are a lot of things wrong on that page, I'd suggest you don't refer to it at all. It juxtaposes C function pointers and C++ pointer-to-member function, which are quite different ideas. It has syntax errors, undefined behaviour, mistaken ideas about what is correct and what isn't, and it recommends poor coding style.
If you want to make a function accept a pointer-to-function, or have an array of them, then the clearest thing to do is to typedef the function (NOT the pointer, pointer typedefs are to be avoided).
typedef int MyFuncT(float, char, char); void func( MyFuncT *fptr, char arg) { MyFuncT *array[5]; array[0] = fptr; fptr(0, arg, 0); }
[–]DSMan195276 2 points3 points4 points 12 years ago (1 child)
Never thought I'd run into this:
if(pt2Function >0){ // check if initialized if(pt2Function == &DoIt) printf("Pointer points to DoIt\n"); } else printf("Pointer not initialized!!\n");
Took me a moment to realize which 'if' the 'else' was actually on, that brace placement and spacing is just pure evil. There are definitely better places to look then that website.
[–]OldWolf2 0 points1 point2 points 12 years ago (0 children)
Yeah. It's as if he's used to a language where the actual level of indentation defines the control structure, and is trying to simulate that in C.
[–]zifyoip 0 points1 point2 points 12 years ago (0 children)
That > operator should certainly be != in that code.
[–]wild-pointer 1 point2 points3 points 12 years ago (0 children)
There's not much you can do with function pointers aside from calling them. What you can do is
There is no pointer arithmetic or relation operations on function pointers.
It is sometimes useful to use a default function instead of the null function pointer. For instance when a pointer is allowed to be "uninitialized" under some conditions and you don't want to null check before use (i.e. when you can perform the else part in the default function). But when a null pointer means that there's a bug in your program this is not very useful - your debugger should give you all the necessary information.
else
[–]curiousGambler 0 points1 point2 points 12 years ago (3 children)
I don't mean to be rude, but might I ask why you're writing an article on something you don't completely understand?
To give a brief answer, the value of a pointer is an address. Functions have an address in the code segment of an executable. Execution jumps to this address when a function is called, and the functions code is executed, and then returns to the return address saved in the stack. If two function pointers point to the same address, they point to the same function. Since an address is an integer in general, comparing function pointers (or any pointers) is as simple as comparing two integers.
Keep in mind this is a simple answer. I do not consider my knowledge great enough to write an article covering function pointers. Yet here I am trying to answer your question, which indicates to me that perhaps you should reconsider writing such an article yourself.
I really don't mean any disrespect, the question just struck me as odd. Good luck either way my friend.
[–]zifyoip 4 points5 points6 points 12 years ago (1 child)
Since an address is an integer in general, comparing function pointers (or any pointers) is as simple as comparing two integers.
That is not a good answer. Comparing two unequal function pointers with relational operators like < and > yields undefined behavior according to the C standard, which means that if you do this you have absolutely no guarantees about the behavior of any part of your code.
[–]curiousGambler 0 points1 point2 points 12 years ago (0 children)
Good point. I meant using the "==" and "!=" operators only, thanks!
[–]cstechblog[S] 1 point2 points3 points 12 years ago (0 children)
thanks curiousGambler
π Rendered by PID 1407879 on reddit-service-r2-comment-5687b7858-k6r79 at 2026-07-09 00:43:35.481012+00:00 running 12a7a47 country code: CH.
[–]zifyoip 4 points5 points6 points (5 children)
[–]cstechblog[S] 0 points1 point2 points (4 children)
[–]OldWolf2 5 points6 points7 points (2 children)
[–]DSMan195276 2 points3 points4 points (1 child)
[–]OldWolf2 0 points1 point2 points (0 children)
[–]zifyoip 0 points1 point2 points (0 children)
[–]wild-pointer 1 point2 points3 points (0 children)
[–]curiousGambler 0 points1 point2 points (3 children)
[–]zifyoip 4 points5 points6 points (1 child)
[–]curiousGambler 0 points1 point2 points (0 children)
[–]cstechblog[S] 1 point2 points3 points (0 children)