函数指针和指针函数一直在工做中会用到,如今mark下。部份内容是参考其余人的总结。ide
int (*f) (int x); /* 声明一个函数指针 */函数
f=func; /* 将func函数的首地址赋给指针f */spa
指向函数的指针包含了函数的地址,能够经过它来调用函数。指针
声明格式以下:
类型说明符 (*函数名)(参数)
其实这里不能称为函数名,应该叫作指针的变量名。这个特殊的指针指向一个返回整型值的函数。指针的声明类型和它指向函数的声明保持一致。
指针名和指针运算符外面的括号改变了默认的运算符优先级。若是没有圆括号,就变成了一个返回整型指针的函数的原型声明。
例如:
void (*fptr)();
把函数的地址赋值给函数指针,能够采用下面两种形式:
fptr=&Function;
fptr=Function;
取地址运算符&不是必需的,由于单单一个函数标识符就标号表示了它的地址,若是是函数调用,还必须包含一个圆括号括起来的参数表。
能够采用以下两种方式来经过指针调用函数:
x=(*fptr)();
x=fptr();code
在FFMPEG 3.2 版本中找到一个函数指针:orm
/** * Read the format header and initialize the AVFormatContext * structure. Return 0 if OK. 'avformat_new_stream' should be * called to create new streams. */ int (*read_header)(struct AVFormatContext *);
定义函数flv_read_header();ip
static int flv_read_header(AVFormatContext *s) { FLVContext *flv = s->priv_data; int offset; avio_skip(s->pb, 4); avio_r8(s->pb); // flags s->ctx_flags |= AVFMTCTX_NOHEADER; offset = avio_rb32(s->pb); avio_seek(s->pb, offset, SEEK_SET); avio_skip(s->pb, 4); s->start_time = 0; flv->sum_flv_tag_size = 0; flv->last_keyframe_stream_index = -1; return 0; }
把函数的地址赋值给函数指针:get
AVInputFormat ff_flv_demuxer = { .name = "flv", .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"), .priv_data_size = sizeof(FLVContext), .read_probe = flv_probe, .read_header = flv_read_header, .read_packet = flv_read_packet, .read_seek = flv_read_seek, .read_close = flv_read_close, .extensions = "flv", .priv_class = &flv_class, };
经过指针调用函数:input
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options) { ... ... if (!(s->flags&AVFMT_FLAG_PRIV_OPT) && s->iformat->read_header) if ((ret = s->iformat->read_header(s)) < 0) goto fail; ... ... }
函数指针本质上是一个指针变量,能够经过它来调用函数原型
类型标识符 *函数名(参数表)
int *f(x,y);
首先它是一个函数,只不过这个函数的返回值是一个地址值。函数返回值必须用同类型的指针变量来接受,也就是说,指针函数必定有函数返回值,并且,在主调函数中,函数返回值必须赋给同类型的指针变量。
表示:
float *fun();
float *p;
p = fun(a);
在FFMPEG 3.2 版本中找到一个指针函数:
static char *get_ost_filters(OptionsContext *o, AVFormatContext *oc, OutputStream *ost) { AVStream *st = ost->st; if (ost->filters_script && ost->filters) { av_log(NULL, AV_LOG_ERROR, "Both -filter and -filter_script set for " "output stream #%d:%d.\n", nb_output_files, st->index); exit_program(1); } if (ost->filters_script) return read_file(ost->filters_script); else if (ost->filters) return av_strdup(ost->filters); return av_strdup(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? "null" : "anull"); }
//定义指针变量 char *avfilter; //函数返回值指向指针变量 avfilter = get_ost_filters(o, oc, ost);
指针函数:本质上是函数,返回值为指针变量。