Ç - 如何将指针转换成一个数组索引? [英] C - how to convert a pointer in an array to an index?

查看:108
本文介绍了Ç - 如何将指针转换成一个数组索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在的C(bsearch想到)如果结果是发现了许多搜索功能,则返回一个指针阵列中的点。我怎么能这个指针转换为搜索到的数组索引(使用指针运算,我假设)。


解决方案

  ptrdiff_t的指数= pointer_found  -  ARRAY_NAME;

In the many search functions of C (bsearch comes to mind) if a result is found, a pointer to the spot in the array is returned. How can I convert this pointer to the index in the array that was searched (using pointer arithmetic, i assume).

解决方案

ptrdiff_t index = pointer_found - array_name;

这篇关于Ç - 如何将指针转换成一个数组索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆