Thread: How to call a function from a function pointer

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    How to call a function from a function pointer

    Hi everyone. I'm learning C99.

    K&R second edition, 5th chapter, seems to give the following syntax for calling a function from a function pointer:

    void (*fn)() = myfunction;
    (*fn)();

    yet, my compiler seems to accept:

    fn();

    Is the latter valid C, or an extension? TIA,

    Richard

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Valid C.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    This is one of those funny areas of C.

    An expression with a function type is, in almost all cases, converted to a pointer to a function. Thus the function call operator () is defined as operating on a pointer to a function, since it can never actually operate on a function.

    So you have fn, which is a pointer to a function. Therefore you can use the () operator on it, since that's how the () operator works. If you apply the * operator to fn, you get a function, which makes sense. But due to the above rule, it immediately turns back into a function pointer. So (*fn)() works, too. The best part of this (well, the most amusing part) is that since *fn winds up being a function pointer, you can apply the * operator to it, yielding a function, which is immediately converted to a function pointer.... and on and on. So you could, if you wanted, do (*************fn)().

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    So how should I do it?

    So what's the 'proper' way to invoke a function from a function pointer?

    (*fn)();

    or

    fn();

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Richardcavell View Post
    So what's the 'proper' way to invoke a function from a function pointer?

    (*fn)();

    or

    fn();
    The latter. The former just makes it harder to read.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Richardcavell View Post
    So what's the 'proper' way to invoke a function from a function pointer?

    (*fn)();

    or

    fn();
    Depends who you ask.

    I believe that K&R C used the former form, e.g. (*fn)().
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Both are 'proper'. In my opinion, the (*fn)() form makes it clear you're using a function pointer, though I don't like unnecessary syntax cluttering up my code. The fn() version make it look nice and clean, like a regular function call, but can cause you or others to miss the fact that fn is a function pointer and that could be a serious problem (this reminds me a bit of the old debate on Hungarian notation). Try both and see what works best for you. If you end up in a class or job that requires one or the other, you'll adapt quick enough.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The short form (fn()) was not supported in K&R C, except as an extension with some compilers. However, in standard C, both the short and long forms are supported.

    So, if you anticipate using antique compilers, prefer the long form. Otherwise, use which ever style works best for you.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Richardcavell View Post
    So what's the 'proper' way to invoke a function from a function pointer?

    (*fn)();

    or

    fn();
    Either works... but why make it more complex than it needs to be?

    As has been explained already, when you call a function in C you are actually creating a machine code subroutine call in the CPU... so it's all "call by address", the names are simply a convenience for us wee humans... It's just that in setting up the prototype for a redirectable function call (function pointer) this process is suddenly visible to you as a programmer. However; once the address is set for your function pointer, it works just like any other function call.

    Personally... I always use the latter form... function();

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CommonTater View Post
    Either works... but why make it more complex than it needs to be?
    This actually comes down to the reason K&R C didn't support the short form originally.

    It's because calling a function via a pointer and calling a function directly in code, translates to different machine instructions on all architectures.

    Indirection (such as accessing something through a pointer) involves more operations - specifically to access the value of the pointer before dereferencing it - than accessing it directly. That means a small performance differential, but one that can be important in some contexts - such as inside a tight loop.

    K&R required different syntax, as - in practice - the hit associated with calling via function pointer was fairly significant. That is less of an issue with modern hardware than it was then.

    The choice of syntax that is still in the standards - apart from backward compatibility - allows a programmer to make the difference explicit - or not - as appropriate.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  4. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  5. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM

Tags for this Thread