2009-11-12 5 views

답변

1
structure2 *s2 = (structure2*)malloc(sizeof(structure2)); 
s2->ptr = (structure1*)malloc(sizeof(structure1)); 
s2->ptr->fptr = foo; 
char x = 'a'; 
s2->ptr->fptr(&x); 
+0

죄송합니다 .. 고정 :) – Amarghosh

0
  • 유형 그것에 structure2
  • 지정 structure1 할당 위의 유형 structure1의 객체의 주소 (이 꽤 몇 가지 방법으로 수행 할 수 있습니다)
  • 지정 foo의 객체를 생성 객체의 fptr 멤버 사용
  • 전화 foo :

    structure2 s2; 
    // allocate 
    char c = 42; 
    s2.ptr->fptr(&c); // if this 
    

예 :

typedef struct 
{ 
    int *a; 
    char (*fptr)(char*); 
}structure1; 

typedef struct 
{ 
    int x; 
    structure1 *ptr; 
}structure2; 

char foo(char * c) 
{ 
return 'c'; 
} 

int main() 
{ 
structure1 s1; 
structure2 s2; 
s1.fptr = foo; 
s2.ptr = &s1; 
char c = 'c'; 
printf("%c\n", s2.ptr->fptr(&c)); 
return 0; 
} 
관련 문제