一.函数指针,指针指向函数
//
// main.c
// cdemo
//
// Created by liyongkai on 2021/6/6.
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
int func(int a, char b) {
printf("hello world\n");
return 1;
}
int main(int argc, const char * argv[]) {
//1.使用typedef
typedef int (FUNC_TYPE)(int, char);
FUNC_TYPE *p = func;
//以下三种方式都能正确打印
func(1, 'c');
p(1, 'c');
(*p)(1, 'c');
/**
p(1, 'c');
(*p)(1, 'c');
直接调用p和使用 *p的结果是一样的,是因为直接调用p的话系统会帮我们调用*p.
*/
//2.使用typedef + *
typedef int (*P_FUNC_TYPE)(int, char);
P_FUNC_TYPE pp = func;
pp(1, 'c');
//3.直接引用
int (*pFunc)(int, char) = func;
pFunc(1, 'c');
return 0;
}
二.函数指针做参数
网友评论