How Call a function using it's pointer?

This is a discussion on How Call a function using it's pointer? within the C++ Programming forums, part of the General Programming Boards category; If a make a function and one of it's params takes a function pointer, how do I call the function ...

  1. #1
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102

    How Call a function using it's pointer?

    If a make a function and one of it's params takes a function pointer, how do I call the function that is passed as an argument?
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    How do you mean? you can just place the function call of one function inside that of another as long as it returns the same value as the parameter of the first.
    Code:
    int addnum(int a, int b)
    {
         return(a+b);
    }
    
    printf("%d", addnum(10, 5));
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    Oh nice, preview my message, then hit back to edit and it's gone..
    let's try again

    I want to do the implementation of a function that calls the callback function.

    RETURN_TYPE FunctionName(some params, function pointer parameter)
    {
    do some stuff;

    call the function using it's passed function pointer;//this is what I can't find an example of

    return whatever;

    }
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  4. #4
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    this code checks if two strings are the same... basically it's a wrapper for cmpstr...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void check (char *a, char *b, int (*cmp) ());  // how to declare the function to accept the function pointer
    
    int main (void)
    {
      char s1[80], s2[80];
      int (*p)(); // function pointer
    
      p = strcmp;
    
      gets (s1);
      gets (s2);
    
      check (s1, s2, p);
    }
    
    void check (char *a, char *b, int (*cmp) ())
    {
      printf ("testing for equality\n");
      if (
        !(*cmp) (a, b)  //  this is how you call the function via it's pointer...
        )
        printf ("equal");
      else
        printf ("not equal");
    }
    any ?uestions?
    hasafraggin shizigishin oppashigger...

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    On most compilers you should be able to call it like a standard function - cmp(a,b);
    zen

  6. #6
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    Thanks to both of you

  7. #7
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    but you need to dereference it... no?
    hasafraggin shizigishin oppashigger...

  8. #8
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    No need to dereference it


    int f( int x, FUNCTION_POINTER p )
    {
    return p( x );
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  9. #9
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    WHAT! you must be trippin' nv! but... but.... why?!?!?!?!
    hasafraggin shizigishin oppashigger...

  10. #10
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Because having function values is impossible.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  11. #11
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    well that's true, but it's the nature of pointers tho... since they contain addresses [in these cases a code address]... and, actually, couldn't you change the function pointer's pointing address manually?
    hasafraggin shizigishin oppashigger...

  12. #12
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    Since we're on the topic. When you call a function, any memory used by ints bools or other variables that are declared in the function is freed up once the function returns a value, correct?
    How is a pointer or a class any different in that it needs to be dereferenced or decunstructed?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  4. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  5. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM

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