Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Function Vector

DZone's Guide to

Function Vector

·
Free Resource
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:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}