Like Tree1Likes
  • 1 Post By Salem

Function pointers in structure

This is a discussion on Function pointers in structure within the C Programming forums, part of the General Programming Boards category; Hi, I have a func pointer in a structure like this- Code: struct mystruct { int (*fp)(int,int); }mystruct; I initialize ...

  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
    32,855
    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.
    dmh2000 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    22,308
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11:57 AM
  3. How To Detect Pointers In Structure
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 07-31-2003, 05: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, 12:04 PM

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