Thread: function pointers

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

    function pointers

    hi,
    the first of my question is about pointers to functions

    i dont even know why do we need them
    cant we just use functions as one of the arguments to another function

    if we need to use pointers to functions to use as them arguments how do we declare these pointer and what is the right syntax to pass function pointers to functions

    do we have to use void as pointer's function arguments like

    int(*comp)(void*,void *)

    are function names, pointers to their functions?

    i would be very grateful if you could tell me how to define and use pointers to functions

    my second question is
    what is this

    ((void**)lineptr)

    if lineptr is declared like this

    char *lineptr[]
    a character pointer array


    is this a redeclaration of the type this array?
    can we rearrange the types like this


    thank you

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are many different reasons to use function pointers. Some of those are:
    - CallBack functions where you pass a function to another function, and the called function doesn't know how to do something (e.g a generic sort function given a compare function for the data to be sorted - that way you can sort up, down, alphabetical order, length order or some other order by just changing the compare function, not the sort function). Another typical callback operation is validation of data-input - say you have a function to read a string from the user, and you want to use it to confirm that the user has input something that is a valid-looking e-mail address, phone number or name - so pass along a function that can determine if the string looks like (e-mail, phone number or name) - e.g. a persons name shouldn't contain numbers or special characters, e-mail address should contain @, phone number should start with X and then contain Y number of digits with optional spaces and dashes, possibly + or parenthesis thrown in for good measure).

    - Table-based programming, e.g. you build a table of menu-choices (strings, key-presses, selections in a (X-)Windows menu or such), along with the action for each menuitem as a function pointer.

    - Instance-based differences (sort of like member functions in object orientation): Say you have a generic function to draw graphical objects, and you pass along a function that knows how to draw the specific instance (circle, square, oval, rectangle, etc).

    - Hardware/software architecture differences - a function pointer passed points to either the SSE or "regular math" version of a function to do the same thing, or one version of Windows has a clever way to do something, whilst in the older version, the same thing is either not availabe (so you do a simpler thing) - have a function pointer that points to the "advanced" or "simple" version.

    These are just some of the categories of function pointer use.

    This
    Code:
    ((void**)lineptr)
    looks like a type-cast, where some function is expecting a pointer to a pointer of void, but the actual type coming out of the previous declaration makes it pointer to pointer of char (the address of lineptr[0], and lineptr[0] is a pointer to char, which makes the whole thing a pointer to pointer of char).

    --
    Mats

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    thanks matsp but why void **

    does it change the type of the lineptr pointer's adress which is a char
    and if that is so why not

    (void *)(* lineptr)

    and cant we just use functions as arguments

    and are function names, pointers to that functions
    Last edited by benhaldor; 08-19-2007 at 09:16 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In the case of the cast, all it does is change the type (basicly saying "Yes, I know you, the compiler, thinks it's a char **, but I say now that it's a void **") - it doesn't change the value, just what type it is.

    You can do "void *p = (void *) 1000;" - this casts the integer 1000 to a void pointer - so p would have the value 1000.

    Function pointers are variables that hold the address of a function. A function name is an identifier of a function - it "is" the address of the function, just written with words - it would be hard to remember which is which if you have functions "called" 1234, 1243 a 1324 for example.

    So if we have a function called "abc", that has the address of 1234, and a function pointer called "fp", this is what the code would look like:
    Code:
    void (*fp)(void);
    
    void abc(void)
    {
        /// ... do something useful here .. 
    }
    
    ...
    int main(void)
    {
       ... 
       fp = abc;  // This sets fp to the value 1234 (address of abc). 
    
       fp();    // calls abc. 
       (*fp)();  // alternative stype for fp();
       abc();  // call abc directly.  
       // All of the above three lines do exactly the same thing. 
    }
    --
    Mats

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    thank you very much matsp

    but ididnt understand this
    (*fp)(); // alternative stype for fp();

    doesnt (*fp) already call for abc function because it is a pointer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  4. 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
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 04:22 PM