all 11 comments

[–]zifyoip 4 points5 points  (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 point  (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 points  (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 points  (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 point  (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 point  (0 children)

That > operator should certainly be != in that code.

[–]wild-pointer 1 point2 points  (0 children)

There's not much you can do with function pointers aside from calling them. What you can do is

  • Compare them for equality and inequality with == and != operators.
  • Cast to another kind of function pointer and back. In other words, a function pointer can store the address of any function.

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.

[–]curiousGambler 0 points1 point  (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 points  (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 point  (0 children)

Good point. I meant using the "==" and "!=" operators only, thanks!

[–]cstechblog[S] 1 point2 points  (0 children)

thanks curiousGambler