Thread: Structure with a pointer to a function

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    2

    Question Structure with a pointer to a function

    The following code I need help with

    Code:
    struct {
      int x;
      int y;        
      int (*func)(int,int);
    } test;
    
    int testFunc(int a,int b)
    {
    
     return b;
    } 
    
    void testing()
    {
       test.x=1;
        test.y=2;
        test.func=&testFunc(5,6);
    
        
    }
    
    int main(int argc, char **argv)
    {
      testing();
    
       return 0;
    }
    I am getting the following error

    Code:
    error: lvalue required as unary ‘&’ operand
      test.func=&testFunc(5,6);
    Last edited by Phil_Lid; 02-13-2019 at 07:06 AM. Reason: mistake on [/code]

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Like array names, a pointer to a function is just it's name.
    test.func=testFunc;

    To call the function, you write
    int result = test.func(5,6);
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-04-2017, 07:42 AM
  2. Replies: 3
    Last Post: 03-14-2013, 03:25 PM
  3. structure array pointer function help
    By BrandNew in forum C Programming
    Replies: 4
    Last Post: 03-07-2011, 10:48 PM
  4. Function to return pointer to structure
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 06-25-2002, 06:10 AM

Tags for this Thread