Thread: Function pointers in structure

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    24

    Question Function pointers in structure

    Hi,
    I have a func pointer in a structure like this-
    Code:
    struct mystruct
    {
        int (*fp)(int,int);
    }mystruct;
    I initialize this func pointer in a function like this-
    Code:
    void Init(mystruct *handle,int (*my_fp)(int,int))
    {
        handle->fp=my_fp;
    }
    thenI'm trying to use this function pointer in another function, like this-
    Code:
    int func(mystruct *handle)
    {
       int c=handle->fp(1,2);
       return c;
    }
    Somehow, it is giving me a lot of syntax errors. Please tell me what I'm doing wrong here!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps you meant
    Code:
    typedef struct mystruct
    {
        int (*fp)(int,int);
    }mystruct;
    As written, you're just creating a global variable called mystruct with a type of struct mystruct.
    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
    Aug 2012
    Posts
    24
    Yes. I'm sorry. I did include the typedef in my original code. It is not working. Please help me out here!!!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sakura
    Yes. I'm sorry. I did include the typedef in my original code. It is not working. Please help me out here!!!
    Okay. So I took the code you posted, added the typedef, added a function so that I can pass the function pointer to Init, then wrote a main function to call your Init and func functions, and it works for me:
    Code:
    #include <stdio.h>
    
    typedef struct mystruct
    {
        int (*fp)(int, int);
    } mystruct;
    
    void Init(mystruct *handle, int (*my_fp)(int, int))
    {
        handle->fp = my_fp;
    }
    
    int foo(int x, int y)
    {
        return x + y;
    }
    
    int func(mystruct *handle)
    {
       int c = handle->fp(1, 2);
       return c;
    }
    
    int main(void)
    {
        mystruct obj;
        Init(&obj, foo);
        printf("%d\n", func(&obj));
        return 0;
    }
    The program compiles and prints "3".

    As you can see, claiming that "somehow, it is giving me a lot of syntax errors" without also showing us the code that results in those errors and telling us what those errors are makes you look stupid when someone uses the snippets of code that you posted and proceeds to demonstrate that you are mistaken.
    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

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    24
    Thanks for the tip!

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    24
    Yup. My bad!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function arguments, pointers to a structure...
    By \007 in forum C Programming
    Replies: 13
    Last Post: 12-07-2010, 03:26 AM
  2. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  3. How To Detect Pointers In Structure
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 07-31-2003, 04:12 PM
  4. Passing Structure Pointers
    By mattz in forum C Programming
    Replies: 12
    Last Post: 11-18-2001, 06:23 AM
  5. structure pointers
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 11:04 AM