-
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.
-
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.