从bsearch和lfind确定索引? [英] Determining index from bsearch and lfind?

查看:287
本文介绍了从bsearch和lfind确定索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在lfind和bsearch之后返回数组中的元素的索引,返回它找到的元素的指针。我到目前为止:

I'm trying to get the index of the element in the array after lfind and bsearch return the pointer to the element that it found. I have this so far:

(char *) (found - cv->baseAddress); 

其中找到的是找到的函数的地址,基址是元素0的地址。但是,编译器给我这个错误:

where found is the address of what the functions found, and the base address is the address of element 0. However, the compiler gives me this error:

cvector.c:150:28:warning:在减法中使用类型void *的指针
cvector .c:150:4:warning:return从不带转义的指针返回整数

cvector.c:150:28: warning: pointer of type ‘void *’ used in subtraction cvector.c:150:4: warning: return makes integer from pointer without a cast

我该怎么办?

推荐答案

您必须将返回的指针类型转换为正确的类型。然后你还需要将你获得的偏移量与数组中对象的大小相除,得到索引。

You have to typecast the returned pointer to the correct type. Then you also need to divide the offset you get with the size of the objects in the array to get the index.

这篇关于从bsearch和lfind确定索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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