Hello. I have a small problem that I can not resolve.
What I would like to do is simply initialize a struct that receives function pointers automatically. For example:
struct teste{
int x;
int y;
int (*soma)(int, int);
};
typedef struct teste Teste;
int soma(int a, int b){
return a + b;
}
Teste *t1;
t1->soma = soma;
printf("---> 2 + 2 = %d \n", t1->soma(2, 2)); // ---> 2 + 2 = 4
This compiles and performs very well. However, I would not want to do the following:
Teste *t1;
t1->soma = soma;
And just do something like:
Teste *t1;
*t1 = newTeste();
Or even:
Teste *t1 = newTeste();
Since the newTeste () function would look something like:
Teste newTeste(){
Teste *t;
t->soma = soma;
return *t;
}
This compiles but does not spin.
I know I'm getting lost in pointers, but I can not see exactly where. I also do not know if what I want to do is something viable, I just came up with this idea of micro simulate a class and I would like to put it into practice.
Thanks in advance.