Thread: *function

  1. #1
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685

    *function

    I have been programming long enough to know what is correct....but

    Code:
    typedef int (*pfunc)(void);
    
    int some_func(int arg, pfunc func) {
        int i = (*func)();  //correct
        ...
    }
    But here is where I am unclear:

    Code:
    typedef int (*pfunc)(void);
    
    int some_func(int arg, pfunc func) {
        int i = func();  //incorrect but works
        ...
    }
    The second example does work. As a typical programmer who tends to cut corners from time to time I would like to know if there is any disadvantage to not dereferrencing a call to a pointer to a function?

  2. #2
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31
    I think a function name, much like an array name, is just a pointer.

    Code:
    int func( void );
    func is a pointer to a function, with no arguments, returning int.

    Code:
    int a = func( );
    int a = (*func)( );
    Both statements above are just the same.
    Last edited by char; 10-21-2002 at 05:50 AM.

  3. #3

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Sweet, the book and the link say the same thing for those of you who don't own the book. In my opinion--as if anyone cares, I prefer to not dereferrence a pointer to a function. However, I think that it may be a good thing to do when someone else is reading your code (or you in the distant future) as a means of conveying that it is a pointer not an actual function.

    Thanks for setting me straight everyone.

Popular pages Recent additions subscribe to a feed