Thread: Pointer to function problem

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    15

    Pointer to function problem

    Hi,

    So I have three similar working functions:
    Code:
    char *ltrim(char *);
    char *rtrim(char *);
    char *trim(char *);
    And i want to write a function that can test these functions and takes a pointer to function as its argument.

    I tried this first
    Code:
    void test_func(char *(*func)(char *in))
    {
        printf("in = |%s|\nin.length = %d\nout = |%s|\nout.length = %d\n",
    	    in,
    	    strlen(in),
    	    (*func)(in),
    	    strlen((*func)(in)));
    }
    But this won't compile and the compiler says 'in' is undeclared.

    Then I changed it to this
    Code:
    void test_func(char *(*func)(char *),char *in)
    {
        printf("in = |%s|\nin.length = %d\nout = |%s|\nout.length = %d\n",
    	    in,
    	    strlen(in),
    	    (*func)(in),
    	    strlen((*func)(in)));
    }
    And it seemed to work since i could print out the expected results with this
    Code:
    test_func(ltrim, str);
    So my problem is
    1) why the first piece of code won't compile (to be more specific, I'd expected 'in' to be an argument passed to the function that is pointed to. What does the compiler *think* of this variable to be)
    2) Is the second piece of code the right way to do this?

    Thanks,

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    char *(*func)(char *) is a function pointer. It refers to a function by itself. It does not include any arguments. So you can use the same function pointer multiple times with different arguments.

    And yes, your second code is right.

    By the way, you don't need * when you call the function from the pointer. you can just call func(in). It's a syntax convenience.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    1) In the first case 'in' is just part of the function prototype (you aren't actually invoking the function at that point, after all, so how could 'in' actually get passed in?). Think of it as if you had created a typedef:

    Code:
    typedef char* (* function )( char* in );
    
    void test( function fun )
    {
    	fun( "ok" );
    }
    2) That's fine, but you don't actually have to dereference the pointer (see example above). Either way works, though.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    15
    Thank you guys for reply. Still i have a question.
    Quote Originally Posted by King Mir View Post
    char *(*func)(char *) is a function pointer. It refers to a function by itself. It does not include any arguments. So you can use the same function pointer multiple times with different arguments.
    So King Mir what do you mean by "It does not include any arguments."?
    In my understanding char *(*func)(char *) is a pointer to function that takes type char * as argument and returns type char *.

    For the dereferencing part, yes, that's quite cool and also Sebastiani's typedef solution looks neat. I'll try those and thanks for these good suggestions.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vincent01910
    So King Mir what do you mean by "It does not include any arguments."?
    In my understanding char *(*func)(char *) is a pointer to function that takes type char * as argument and returns type char *.
    King Mir means that just because you have a function pointer does not mean you have an argument that can be passed to the function called via that function pointer. In other words, what Sebastiani was talking about in.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    15
    Thanks laserlight. I get it now.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    also, you can just do
    Code:
    void test_func(char *func(char *),char *in)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  2. Pointer in Function problem
    By lilhawk2892 in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 02:41 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. 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