I am trying to understand C syntax and I have done the following example. I have placed a function pointer *func
as a property of my Person
struct, which returns a struct Person
.
typedef struct
{
int age, salary;
struct Person(*func) (int age, int salary);
} Person;
Person other_func(int age, int salary)
{
Person* person = malloc(sizeof(Person));
person->age = age;
person->salary = salary;
return *person;
};
int main()
{
Person p;
p.func= other_func;
p = p.func(30, 3000);
}
This gives me "Can not convert Person to Person" on the last line. I suppose this is because the one is Person
and the second is struct Person
, but inside the Person
struct, I have have my function as struct Person(*func_1) (int age, int salary);
because it raises a compilation time error if I use Person
instead of struct Person
. So I used struct Person
instead. Is this the problem ? How would I achieve what I am trying to do ?