2012-03-25 9 views
10

Bunun yapı içinde bir alan olarak typedef void (*DoRunTimeChecks)();Bir işlev işaretçisini bir yapıya nasıl kaydedebilirim?

nasıl depolayabilir mi ilan etmişlerdir? Nasıl atayabilirim? Fn() öğesini nasıl arayabilirim?

+3

Not:

anExample.y = Function; 

işlevini çağırmak için Bu, C, void fn() ve void fn (void) 'in eş anlamlı değildir. –

+0

+1 teşekkürler. Ben zaten buna düştüm – Mawg

cevap

15

Tıpkı sizin gibi koymak başka herhangi alanı:

struct example { 
    int x; 
    DoRunTimeChecks y; 
}; 

void Function(void) 
{ 
} 

struct example anExample = { 12, Function }; 

alanına atamak için:

anExample.y(); 
4
#include <stdio.h> 

typedef void (*DoRunTimeChecks)(); 

struct func_struct { 
    DoRunTimeChecks func; 
}; 

void function() 
{ 
    puts("hello"); 
} 

int main() 
{ 
    struct func_struct func_struct; 
    func_struct.func = function; 
    func_struct.func(); 
    return 0; 
}