C笔记——typedef和函数指针
在看AMCL源码时,有几处用到函数指针的声明,如下
// Function prototype for the initialization model; generates a sample pose from // an appropriate distribution. typedef pf_vector_t (*pf_init_model_fn_t) (void *init_data); // Function prototype for the action model; generates a sample pose from // an appropriate distribution typedef void (*pf_action_model_fn_t) (void *action_data, struct _pf_sample_set_t* set); // Function prototype for the sensor model; determines the probability // for the given set of sample poses. typedef double (*pf_sensor_model_fn_t) (void *sensor_data, struct _pf_sample_set_t* set);
pf_init_model_fn_t是声明的新名字,也可以写在最后面
typedef pf_vector_t (*) (void *init_data) pf_init_model_fn_t;
pf_init_model_fn_t是函数指针,(*)后面是的括号是函数的形参,void *表示未知类型的指针,可以指向任意类型的变量; (*)前面pf_vector_t值得是这个函数的返回类型。
同样理解
typedef void (*) (void *action_data, struct _pf_sample_set_t* set) pf_action_model_fn_t;
pf_action_model_fn_t是函数指针,指向带两个参数的函数,函数无返回类型。
在《C和指针》P260有讲函数指针,有两个用途:
1、把函数指针作为参数传递给其他函数,后者会“回调”用户的函数
2、转移表。具体例子见书。
作者:水水滴答
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

浙公网安备 33010602011771号