Thread: Function pointer of a function pointer with an argument

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28

    Function pointer of a function pointer with an argument

    I'm trying to use a function pointer that is taking another function pointer with an argument. So f2() is the argument to the first function.

    But I'm getting an error saying "error: lvalue required as unary ‘&’ operand".

    Am I doing something that is'nt possible? Or how shall I implement this?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    typedef void(*funcPtr)(void(*ptr)(int));
    
    
    void f1(void (*op)(int b))
    {
        printf("%s() - \n", __func__);
        (*op)(b);
    }
    
    void f2(int b)
    {
        printf("%s() - b: %d\n", __func__, b);
    }
    
    
    int main()
    {
      funcPtr pF1 = NULL;
      pF1 = f1;
      pF1(&f2(5));
    
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,215
    How about
    Code:
    #include <stdio.h>
    #include <stdlib.h>
      
    typedef void(*funcPtr)(void(*ptr)(int),int);
     
     
    void f1(void (*op)(int b), int b)
    {
        printf("%s() - \n", __func__);
        (*op)(b);
    }
     
    void f2(int b)
    {
        printf("%s() - b: %d\n", __func__, b);
    }
     
     
    int main()
    {
      funcPtr pF1 = 0;
      pF1 = f1;
      pF1(f2,5);
     
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28
    Yes, It's working. Thanks for the tip.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointer as function argument?
    By mtn_student in forum C Programming
    Replies: 1
    Last Post: 09-27-2011, 03:25 PM
  2. Passing address or pointer as argument to function
    By Edelweiss in forum C Programming
    Replies: 7
    Last Post: 08-17-2011, 12:38 AM
  3. Using a function pointer as a function argument
    By evansm84 in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2010, 10:40 PM
  4. pointer as function argument
    By bertazoid in forum C Programming
    Replies: 1
    Last Post: 09-23-2010, 10:27 AM
  5. pointer and function argument question.
    By stevfletchcom in forum C Programming
    Replies: 5
    Last Post: 06-14-2010, 11:07 PM

Tags for this Thread