取消引用指向 void 的指针
我正在用 C 语言实现 hashmap,CMapPut
的函数定义如下:
void CMapPut(CMap *cm, const char *key, const void *elemAddr)
我的问题是如何检索传递到映射中的元素的实际值?也就是说,客户端传入变量的时候,他传入的是值的地址。在这种情况下,它似乎是一个 void *
,当然您不能取消引用 void *
。有什么建议吗?
I am working on an implementation of a hashmap in C, and the function for CMapPut
is defined as follows:
void CMapPut(CMap *cm, const char *key, const void *elemAddr)
My question is how do I retrieve the actual value of the element passed into the map? That is, when the client passes in the variables, he passes in the address of the value. In this case it appears to be a void *
though, and of course you can't dereference a void *
. Any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够将指针转换为您需要的类型:
我想您会将指针存储为 const void* 。我猜你有一个像
CMapGet
这样的函数,你可以用它来检索映射的对象,如下所示:You should be able to cast the pointer to the type you need:
I guess you'll store the pointer as
const void*
. I guess you have a function likeCMapGet
, you use to retrieve your mapped objects like this:正如其他人提到的,将您的
void *
转换为适当的类型。其次,您不必const
您的属性elemAddr
。这是没有用的,因为你可能想返回一个非常量指针。Cast your
void *
to your appropiate type, as mentioned by others. Second, you don't have toconst
your attributeelemAddr
. It's no use, since you probably want to return a non-const pointer anyway.