arrays - function pointers for objects in C -


typedef struct node{         int term;         struct node *next; }node; typedef void(*ptr )(void *); typedef void(*ptr1)(void *,int,int); typedef int(*ptr2)(void *,int); typedef void(*ptr3)(void *,int); typedef void(*ptr4)(void *,void *,void *);  typedef struct list{       node *front,*rear;       ptr3 insert;       ptr *many;       ptr display,sort,read;       ptr4 merge; }list;  void constructor(list **s) {     (*s)=calloc(1,sizeof(list));     (*s)->front=(*s)->rear=null;     (*s)->insert=push_with_value;     (*s)->read=read;     (*s)->sort=sort;     (*s)->display=display;         (*s)->merge=merger;          (*s)->many=calloc(2,sizeof(ptr));     (*s)->many[1]=read;    } int main()  {     list *s1,*s2,*s3;     constructor(&s1);     constructor(&s2);     constructor(&s3);      s1->many[1](s1);     s1->sort(s1);     s1->display(s1);     return 0; } 

the void * parameter in such functions gets typecast list * inside function. there way through can call s1->readit; changing many[1] name read_it;?

i intend create common header file, can use programs. since don't know how many function pointers need intend create dynamic array of each function pointer type.

typedef struct list{   node *front,*rear;   ptr3 insert;   ptr readit;   ptr display,sort,read;   ptr4 merge; }list; 

...

(*s)->readit = read; 

...

s1->readit(s1); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -