Over a million developers have joined DZone.

Function Vector

·
This Code create a vector of functions.


#include 
#include 


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;
}

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}