DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Function Vector

09.24.2006
| 6349 views |
  • submit to reddit
        This Code create a vector of functions.

#include <stdio.h>
#include <stdlib.h>


typedef struct _clase{
        int a;
        void(*func[3])(struct _clase*);
} clase;

void clase_func1 (struct _clase *p){
            printf("Funcion 1: %i\n",p->a);
}

void clase_func2 (struct _clase *p){
            printf("Funcion 2: %i\n",p->a);
}

void clase_func3 (struct _clase *p){
            printf("Funcion 3: %i\n",p->a);
}

void holaMundo(){
     printf("Hola Mundo!\n");
}

typedef void(*func) (void);


int main(int argc, char *argv[])
{
  func val;
  clase c;
  
  c.func[1]=&clase_func1;
  c.func[2]=&clase_func2;
  c.func[0]=&clase_func3;
      
  val=&holaMundo;
  val();
  c.a=1;
  c.func[0](&c);
  c.a=2;
  c.func[1](&c);
  c.a=3;
  c.func[2](&c);
  system("PAUSE");	
  return 0;
}