2017-09-10 38 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 
};