Thread: pointers to function question

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    pointers to function question

    say I have two compare functions one is a compareInt(char* s1, char* s2) and compareString(char* rp1, char* rp2)

    my instructor used this typedef:

    typedef int (*COMPAREFUNC)(void*, void*);
    COMPAREFUNC compare;

    can someone explain what this typedef means??

    here's what he does when we wan't to compare int:

    Code:
    compare = (COMPAREFUNC) &compareInt
    and if we want to compareString

    Code:
    compare = (COMPAREFUNC)& compareString
    I don't get all this, can someone help me

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    typedef int (*COMPAREFUNC)(void*, void*);
    This creates a typedef that says "type COMPAREFUNC is a function pointer to a function that returns an int and takes 2 arguments, the first being of type void* and the second of type void*".

    Quote Originally Posted by -EquinoX- View Post
    COMPAREFUNC compare;
    What this does is define a variable that holds the address to such a function that COMPAREFUNC is a typedef for.


    Code:
    compare = (COMPAREFUNC) &compareInt
    Code:
    compare = (COMPAREFUNC)& compareString
    What these do is just assign the specific functions addresses to the pointers so you can call them later via the pointer.
    However, I would suggest you drop the (COMPAREFUNC) part because it shouldn't be needed. It can also mask a compile error if you try to assign the address of an incompatible function.
    Last edited by Elysia; 04-28-2008 at 01:00 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    It's so you can do this:
    Code:
    compare = compareInt;
    compare(a_thing, another_thing); //calls compareInt
    compare = compareString;
    compare(something, something_else); //calls compareString
    http://www.cprogramming.com/tutorial...-pointers.html
    Last edited by robwhit; 04-28-2008 at 01:13 PM.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by robwhit View Post
    What are comparetInt and compareString?
    some functions with the following prototypes
    Code:
    int compareInt(char* s1, char* s2); /* I do not really know why int comparator wants char* as parameters */
    int compareString(char* rp1, char* rp2);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Yeah, I missed that. Equinox: See my edit.
    Last edited by robwhit; 04-28-2008 at 08:29 PM.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so basically when I do :

    COMPAREFUNC compare;

    it just basically takes the address of the typedef that I already declared before??

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Not even close, especially since a typedef is not something that can have an address. You are declaring a variable of type COMPAREFUNC.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's just a variable with a type. You aren't assigning anything.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Code:
    compare = (COMPAREFUNC) &compareInt
    compare = (COMPAREFUNC)& compareString
    Why use the address-of operator here? I would think that would generate the address of the address of the functions.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nope.
    someFunc and &someFunc is the same thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Why use the address-of operator here? I would think that would generate the address of the address of the functions.
    No. its just pointer to function, but the address operator is optional, because the function is automatically casted to the pointer to function type in this context
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    This isn't a good example of abstraction because it relies on char * and void * having the same alignment and representation (which is guaranteed). Change it to any other pointer type, and you broke your code.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hence also why casting to a function pointer is bad because it masks such errors.
    You should always use a correct function type. Adapt the actual function declaration.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so COMPAREFUNC here is just like a type? something like an int

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM