#include <stdio.h>
typedef struct {int a; int b; int c;} F;
typedef struct{
int a;
int (*ptr)(F*);
} A;
int set_a(F * obj){
obj->a = 1;
}
int main(){
F a;
A b;
b.a = 0;
b.ptr = set_a;
b.ptr(&a);
printf("%d",a.a);
getchar();
}
this seems correct
but why
#include <stdio.h>
typedef struct{
int a;
int (*ptr)(A*);
} A;
int set_a(A * obj){
obj->a = 1;
}
int main(){
A a;
a.a = 0;
a.ptr = set_a;
a.ptr(&a);
printf("%d",a.a);
getchar();
}
this is incorrect? and
#include <stdio.h>
typedef struct{
int a;
int (*ptr)(A);
} A;
int set_a(A * obj){
obj->a = 1;
}
int main(){
A a;
a.a = 0;
a.ptr = set_a;
a.ptr(&a);
printf("%d",a.a);
getchar();
}
this is correct?
I really wonder
thanks
environment Language C on Visual studio 2012 on Windows 7
In the 2nd and 3rd example you refer to
Abefore (during) the declaration of it:Also, in the 3rd example, you declare
ptras function that receivesA, but then assign it with set_a which receivesA*, which is not valid as well.You need to declare it before:
In the first example you refer to
Fwhich already exists so it is OK.