Thread: passing functions as parameters?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    269

    passing functions as parameters?

    I'm new to C and am working on this project. In there, there's these lines of code...

    Code:
    static int
    smpl_handler(manager man, void *virt_event, void *c_data, att_list attrs)
    {
        simple_rec_ptr loc_event = virt_event;
       printing stuff
    }
    then in main, they have this..

    Code:
    term_action(man, stn_ev, format_list, smpl_handler);
    term_action is a function and they're passing in the other function? smpl_handler has parameters but those aren't passed.. I'm confused as to what's going on here

    also smpl_handler is defined as "static int"... but it's a function that doesn't return any values....
    Last edited by dayalsoap; 08-27-2010 at 12:27 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A pointer to the smpl_handler function is passed such that smpl_handler can be invoked from term_action.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    It's passing a pointer to the function. But because of the exact semantics of function-pointers in C this is often very confusing.

    If you really need more info you could check here here or read up with a C reference book (Kernighan does cover function pointers IIRC).

    EDIT: Beaten to the point. Again.
    Consider this post signed

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by bernt View Post
    It's passing a pointer to the function. But because of the exact semantics of function-pointers in C this is often very confusing.

    If you really need more info you could check here here or read up with a C reference book (Kernighan does cover function pointers IIRC).

    EDIT: Beaten to the point.
    Thanks for the reference! Looks good.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    269
    also, what about the "static int"? The function doesn't return any int...

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Post the function code.
    Probably old K&R style code which does not have void type.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by Bayint Naung View Post
    Post the function code.
    Probably old K&R style code which does not have void type.
    that is the code.. there is just one printf statement, that's it. I think you might be right in that it's older K&R style, as I don't see voids anywhere.

    Is K&R style more portable? This isn't that old of code.. circa 2007.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    static preceding a function means the function is only visible within that file. More info here

  9. #9
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Code:
    printing stuff
    What's up with this?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Passing parameters between templated functions
    By SpaceCadet in forum C++ Programming
    Replies: 1
    Last Post: 11-20-2006, 03:57 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Replies: 1
    Last Post: 02-06-2003, 03:33 PM
  5. functions passing parameters
    By volk in forum C Programming
    Replies: 9
    Last Post: 01-02-2003, 05:49 PM