I am learning the concepts of function pointers in C and was wondering how to use in structures.
Say for example I have:
struct math{
int a;
int b;
int (*ADD)(int, int);
int (*subtract)(int, int);
};
int add(int a ,int b)
{
return (a+b);
}
int main()
{
math M1;
M1.a = 1;
M2.b = 2;
M2.ADD = add;
}
In this the function pointer ADD is pointing to function add. Is there a way to write a function similar to a constructor that will automatically point ADD to add ?
so that if write M1.construct()
it should do inside that
void construct()
{
ADD = add;
}
I dont want to keep writing for each objects the same lines. Is there a way for this ?
structtypes in C.