pointers to function question

This is a discussion on pointers to function question within the C Programming forums, part of the General Programming Boards category; say I have two compare functions one is a compareInt(char* s1, char* s2) and compareString(char* rp1, char* rp2) my instructor ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,185
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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
    Katy, Texas
    Posts
    2,309
    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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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,185
    Correct.

Page 1 of 3 123 LastLast
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, 01:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Replies: 4
    Last Post: 11-23-2003, 06:15 AM

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