函数指针声明与调用分析
hdf5库函数指针和win32函数指针示例
hdf5库:
H5_DLL herr_t H5Aiterate(hid_t loc_id, unsigned *attr_num, H5A_operator_t op,void *op_data)
转到H5A_operator_t定义:
typedef herr_t (*H5A_operator_t)(hid_t location_id/*in*/, const char *attr_name/*in*/, void
*operator_data/*in,out*/);
解释:H5A_operator_t为指向函数的指针,该指针指向的函数参数列表与上同;
调用示例:
//函数声明
herr_t attr_info(hid_t loc_id, const char *name, void *opdata);
//传递函数指针给调用函数
int idx = H5Aiterate(dataset, NULL, attr_info, NULL);
Win32:
typedef struct tagWNDCLASSEXA {
UINT cbSize;
/* Win 3.x */
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCSTR lpszMenuName;
LPCSTR lpszClassName;
/* Win 4.0 */
HICON hIconSm;
} WNDCLASSEXA, *PWNDCLASSEXA, NEAR *NPWNDCLASSEXA, FAR *LPWNDCLASSEXA;
window结构,其中,指针lpfnWndProc指向窗口消息处理函数,指针对象为指向函数的指针:
typedef LRESULT (CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM);
解释:WNDPROC为指向函数的指针,不仅声明了函数参数列表,还指定了函数的调用方式CALLBACK(__stdcall)。
调用示例:
//函数声明
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
//传递函数指针给结构体成员
WNDCLASSA wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;