pointer to function

This is a discussion on pointer to function within the C Programming forums, part of the General Programming Boards category; I need to create a ADT that is non data type specific. To print I was going to point to ...

  1. #1
    jloyd01
    Guest

    pointer to function

    I need to create a ADT that is non data type specific. To print I was going to point to a function provided by the user that would be specific to his data type. This is for an assingment and I will soon become the "user". Can someone tell me how to define everything so it will jive? I don't understand how to send the pointer to my ADT or how to define the ADT function in the prototype.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Here are some bits of information

    Code:
    #include <stdio.h>
    
    // forward declaration for structure
    struct my_struct;
    
    // a pointer to a print function, taking a ptr to an instance
    typedef void (*printfn)( struct my_struct *instance );
    
    // a structure, containing a ptr to a function to print it
    typedef struct my_struct {
        int     a;
        char    b;      // member variables
        printfn print;  // member functions
    } my_struct;
    
    void my_print_function ( my_struct *inst ) {
        printf( "%d %c\n", inst->a, inst->b );
    }
    
    int main ( ) {
        my_struct fred = {
            1, '2', my_print_function
        };
        fred.print( &fred );
        return 0;
    }
    Though it looks to me like C++ would be better for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 08:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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