C:从空指针推断类型 [英] C: Extrapolating type from void pointer
问题描述
说一个函数将空指针作为参数,例如:int func(void *p);
我们如何确定或猜测p指向的类型?
Say a function takes a void pointer as an argument, like so: int func(void *p);
How can we determine or guess the type of what p is pointing to?
推荐答案
通常,您不能这样做.在某些情况下,如果可以保证p所指向的内容,则可以查看该地址处及其之后的内容以进行查找.通常,您的函数应该知道正在传递的内容,除非它只是传递了它.
In general, you can't. In some cases, if there is a guarantee on what p points to, you may be able to look at the contents at and after that address to find out. Generally, your function should know what it's being passed, unless it just passes it along.
C语言中的数据和函数参数只是一堆位.除非您使用这些位中的某些位来告诉您这些位是什么,否则就没有一致的方法来识别它.
Data and function arguments in C are just a bunch of bits lumped together. Unless you use some of those bits to tell you what the bits are, there's no consistent way to identify it.
这篇关于C:从空指针推断类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!