通过FFMPEG代码学习函数指针和指针函数
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
函數(shù)指針和指針函數(shù)介紹
函數(shù)指針和指針函數(shù)一直在工作中會用到,現(xiàn)在mark下。部分內(nèi)容是參考其他人的總結(jié)。
1. 函數(shù)指針是指向函數(shù)的指針變量,即本質(zhì)是一個指針變量。
int (*f) (int x); /*?聲明一個函數(shù)指針?*/
f=func; /*?將func函數(shù)的首地址賦給指針f */
指向函數(shù)的指針包含了函數(shù)的地址,可以通過它來調(diào)用函數(shù)。
聲明格式如下:
??? 類型說明符?(*函數(shù)名)(參數(shù))
????其實這里不能稱為函數(shù)名,應(yīng)該叫做指針的變量名。這個特殊的指針指向一個返回整型值的函數(shù)。指針的聲明類型和它指向函數(shù)的聲明保持一致。
???? 指針名和指針運算符外面的括號改變了默認(rèn)的運算符優(yōu)先級。如果沒有圓括號,就變成了一個返回整型指針的函數(shù)的原型聲明。
????例如:
????????void?(*fptr)();
????把函數(shù)的地址賦值給函數(shù)指針,可以采用下面兩種形式:
????????fptr=&Function;
????????fptr=Function;
????取地址運算符&不是必需的,因為單單一個函數(shù)標(biāo)識符就標(biāo)號表示了它的地址,如果是函數(shù)調(diào)用,還必須包含一個圓括號括起來的參數(shù)表。
????可以采用如下兩種方式來通過指針調(diào)用函數(shù):
????????x=(*fptr)();
????????x=fptr();
在FFMPEG 3.2 版本中找到一個函數(shù)指針:
/*** 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 *);?
定義函數(shù)flv_read_header();
static int flv_read_header(AVFormatContext *s) {FLVContext *flv = s->priv_data;int offset;avio_skip(s->pb, 4);avio_r8(s->pb); // flagss->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; }?把函數(shù)的地址賦值給函數(shù)指針:
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, };?
通過指針調(diào)用函數(shù):
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; ... ... }?
總結(jié):
函數(shù)指針本質(zhì)上是一個指針變量,可以通過它來調(diào)用函數(shù)
?
2. 指針函數(shù)是指帶指針的函數(shù),即本質(zhì)是一個函數(shù)。函數(shù)返回類型是某一類型的指針。
?????類型標(biāo)識符??? *函數(shù)名(參數(shù)表)
????? int *f(x,y);
首先它是一個函數(shù),只不過這個函數(shù)的返回值是一個地址值。函數(shù)返回值必須用同類型的指針變量來接受,也就是說,指針函數(shù)一定有函數(shù)返回值,而且,在主調(diào)函數(shù)中,函數(shù)返回值必須賦給同類型的指針變量。
表示:
float *fun();
float *p;
p = fun(a);
?
在FFMPEG 3.2 版本中找到一個指針函數(shù):
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"); }
?
?
總結(jié):
指針函數(shù):本質(zhì)上是函數(shù),返回值為指針變量。
轉(zhuǎn)載于:https://my.oschina.net/u/2326611/blog/810704
總結(jié)
以上是生活随笔為你收集整理的通过FFMPEG代码学习函数指针和指针函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 支持断线重连、永久watcher、递归操
- 下一篇: 谓词函数、函数对象