2017-09-10 79 views
2
typedef struct foo{ 
    void (*del)(void *toDel); 
    char* (*p)(void *tp) 
} Foo; 

Foo init(char* (*print)(void*),void (*delFunc)(void*)); 

試圖找出如何分配或初始化提供給struct函數指針的參數。初始化結構中的函數指針

回答

3
Foo init(char* (*print)(void *toBePrinted),void (*delFunc)(void *toBeDeleted)) 
{ 
    return Foo{ .del = delFunc, .p = print}; 
} 

這個是什麼?龍形態:

Foo init(char* (*print)(void *toBePrinted),void (*delFunc)(void *toBeDeleted)) 
{ 
    Foo tmp = { 
     .del = delFunc, 
     .p = print 
    }; 
    return tmp; 
} 
+0

did you mean .del = delFunc .p = print? – waffles

+0

@waffles是的,確切地說,對不起 –

+0

是C99還是C11標準符號? – iBug

0

的直線前進(最向後兼容的方法)來定義fooFoo並初始化它:

Foo foo = { /* Mind the order of the following initialisers! */ 
    delFunc, 
    print 
};