2
What I would like is to automatically initialize a struct that receives function pointers. 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 like to not need to do the:
Teste *t1;
t1->soma = soma;
And just do something like:
Teste *t1;
*t1 = newTeste();
Or even:
Teste *t1 = newTeste();
Where the function newTeste()
would be something like:
Teste newTeste(){
Teste *t;
t->soma = soma;
return *t;
}
That compiles but does not wheel.
I know I’m getting lost in pointers, but I can’t see exactly where. I also don’t 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 into practice.