구조체 2를 사용하여 "foo"함수에 액세스하는 프로그램을 작성하십시오.구조체에 함수 포인터가 정의되어 있습니까?
typedef struct
{
int *a;
char (*fptr)(char*);
}structure1;
typedef struct
{
int x;
structure1 *ptr;
}structure2;
char foo(char * c)
{
---
---
---
}
구조체 2를 사용하여 "foo"함수에 액세스하는 프로그램을 작성하십시오.구조체에 함수 포인터가 정의되어 있습니까?
typedef struct
{
int *a;
char (*fptr)(char*);
}structure1;
typedef struct
{
int x;
structure1 *ptr;
}structure2;
char foo(char * c)
{
---
---
---
}
structure2 *s2 = (structure2*)malloc(sizeof(structure2));
s2->ptr = (structure1*)malloc(sizeof(structure1));
s2->ptr->fptr = foo;
char x = 'a';
s2->ptr->fptr(&x);
죄송합니다 .. 고정 :) – Amarghosh
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;
}
이 숙제인가? –