Passing function pointers

This is a discussion on Passing function pointers within the C Programming forums, part of the General Programming Boards category; TraverseWith takes as one of its arguments a pointer to a function that returns an int and takes 3 parameters. ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    81

    Passing function pointers

    TraverseWith takes as one of its arguments a pointer to a function that returns an int and takes 3 parameters. Can someone tell me (in terms of asm) what the compiler does with the code int (*callback)(int index, int item, void *param).
    Code:
    int traverseWith(int array[], size_t length, 
                     int (*callback)(int index, int item, void *param), 
                     void *param)
    Thanks alot!

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    It is a pointer like any other. The compiler loads it into a register and calls it like any other function.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by brewbuck View Post
    It is a pointer like any other.
    *Like any other function pointer.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by Dave_Sinkula View Post
    *Like any other function pointer.
    True.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    What is this code: "int (*callback)(int index, int item, void *param)" really saying?

    What would the asm look like?
    Last edited by keira; 09-28-2007 at 11:58 PM.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Your compiler will give you the best answer. It's not the same everywhere.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by keira View Post
    What is this code: "int (*callback)(int index, int item, void *param)" really saying?

    What would the asm look like?
    The "prototype" that you have for
    Code:
    int (*callback)(int index, int item, void *param)
    is there so that the compiler can check that you have a sensible function as the callback.

    In this case, it says "callback" is a pointer, to a function that returns an in, and the function takes two integer and one void * parameter.

    It really makes no difference to the compiler as such if you were to pass a function pointer to a function that takes one parameter, or 15 parameters - but because it's probably not the right thing to do, we have taught the compiler to tell us when we get it wrong.

    callback itself is just a pointer - it happens to be a pointer to some function, and when it is being used later on in the code.

    But for the call to "traverseWith", all it will do is pick up a 32 (or 64) bit pointer to the function you want to use, and store that so that the traverseWith function can use it to call the callback function.

    Inside traversewith, you'll find some code that looks something like this:
    Code:
          .... 
           mov  param[esp], eax
           push eax
           mov  index[esp], eax
           push eax
           mov  item[esp], eax
           push eax
           mov  callback[esp], eax
           call    eax
         ....
    Appologies for unaligned spacing on the assembler code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Passing a string array to a function using pointers
    By asofaihp in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 11:31 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21