C++多级指针图解

AudioResample **pResample 指针的地址图解
AudioResample **pResample; // pResample 存储 AudioResample* 的地址 
AudioResample *ar = *pResample; // ar 现在指向 AudioResample 结构体
  • pResample → 指向 AudioResample* 的地址 (0x2000)
  • *pResample → 取出 AudioResample*,得到 0x3000
  • ar = *pResamplear 现在存储 0x3000,和 *pResample 指向同一块 AudioResample 结构体的内存

再看一个demo

AVFilterGraph **graph
AVFilterGraph *filter_graph = avfilter_graph_alloc();  
*graph = filter_graph;
  • AVFilterGraph **graph; 声明了一个 AVFilterGraph* 的指针的指针,意思是 graph 是一个指向 AVFilterGraph* 类型的指针。
  • AVFilterGraph *filter_graph = avfilter_graph_alloc(); 通过 avfilter_graph_alloc() 函数创建并分配一个新的 AVFilterGraph 对象,并将其指针赋给 filter_graph

最后一行 *graph = filter_graph;

  • *graph 表示解引用 graph,也就是访问 graph 指向的 AVFilterGraph* 位置。
  • filter_graph 是已经分配好的 AVFilterGraph*,所以 *graph = filter_graph; 这行代码的意思是将 filter_graph 的值(即指针)赋给 graph 指向的内存位置。

具体操作

在调用 avfilter_graph_alloc() 后,filter_graph 指向了一个有效的 AVFilterGraph 对象。而 *graph = filter_graph; 是将这个指针存储在 graph 指向的内存中。换句话说,graph 指向一个指针,并且这行代码让它指向了 filter_graph

总结

*graph = filter_graph; 的作用是:

  • filter_graph 指向的 AVFilterGraph 对象的指针赋给 graph 指向的位置。
  • 在执行此操作后,*graphfilter_graph 会指向相同的 AVFilterGraph 实例。

*graph = filter_graph;赋值后的graph跟filter_graph关系图   

再举个例子

avformat_alloc_output_context2(AVFormatContext **avctx) 
调用是这样的avformat_alloc_output_context2(&outFormatCtx

avformat_alloc_output_context2() 内部:

  • avctx 等于 &outFormatCtx,即 AVFormatContext **avctx = &outFormatCtx
  • *avctx 等于 outFormatCtx,这个函数内部会分配 AVFormatContext 并赋值给 *avctx
  • 这样 outFormatCtx 就会指向新分配的 AVFormatContext 结构体
  • avctx 是指向 outFormatCtx 地址的指针
  • *avctx=null 则 outFormatCtx这个指针指向的地址设为null outFormatCtx这个指针指向的值也为null

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值