Funtion pointer in structure

This is a discussion on Funtion pointer in structure within the C Programming forums, part of the General Programming Boards category; Ok, let's say that you want to define a funtion pointer that accepts a structure as one of it's parameters. ...

  1. #1
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107

    Funtion pointer in structure

    Ok, let's say that you want to define a funtion pointer that accepts a structure as one of it's parameters. Then say you want to use the function type you defined earlier as a member of that structure. I think you can see where this is going... At any rate, is there a more graceful way to pull this off then using a void * type to handle the structure pointer in the funtion pointer definition?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Something like this maybe:
    Code:
    #include <stdio.h>
    
    struct mystruct
    {
      int i;
      int (*fp) (struct mystruct);
    };
    
    int foo(struct mystruct s)
    {
      return s.i;
    }
    
    int main(void)
    {
      int (*fp)(struct mystruct);
      struct mystruct s;
      int i;
      
      fp = foo;
      s.i = 10;
      s.fp = foo;
      
      i = fp(s);
      printf ("%d\n", i);
      
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing structure by reference or pointer?
    By 7force in forum C Programming
    Replies: 8
    Last Post: 12-13-2010, 05:49 PM
  2. method returning a pointer to a structure
    By nacho4d in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2009, 10:01 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Pointer to a structure
    By frenchfry164 in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 05:35 PM
  5. Pointer to next Structure
    By Garfield in forum C Programming
    Replies: 6
    Last Post: 09-16-2001, 03:18 PM

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