C语言 取消引用空指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15468441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dereference void pointer
提问by Saurabh Ghorpade
Even after casting a void pointer, I am getting compilation error while dereferencing it. Could anyone please let me know the reason of this.
即使在转换空指针之后,我在取消引用它时也会收到编译错误。任何人都可以请让我知道这是什么原因。
int lVNum = 2;
void *lVptr;
lVptr = (int*)&lVNum;
printf("\nlVptr[60 ] is %d \n",lVptr[1]);
回答by David Grayson
It doesn't make sense to dereference a void pointer. How will the compiler interpret the memory that the pointer is pointing to? You need to cast the pointer to a proper type first:
取消引用 void 指针是没有意义的。编译器将如何解释指针指向的内存?您需要先将指针转换为正确的类型:
int x = *(int*)lVptr;
回答by teppic
printf("\nlVptr[60 ] is %d \n", *(int*)lVptr);
printf("\nlVptr[60 ] is %d \n", *(int*)lVptr);
This will cast the void pointer to a pointer to an intand then dereference it correctly.
这会将 void 指针转换为指向 an 的指针int,然后正确取消引用它。
If you want to treat it as an array (of one), you could do a slightly ugly ((int *)lVptr)[0]. Using [1]is out of bounds, and therefore not a good idea (as for lVptr[60]...)
如果你想把它当作一个数组(一个),你可以做一个稍微难看的((int *)lVptr)[0]. 使用[1]越界,因此不是一个好主意(至于lVptr[60]...)
回答by Ed S.
It's still a void*because that's what you declared it as. Any pointer may be implicitly converted to a void*, so that cast does nothing and you are left with a pointer to voidjust as you began with.
它仍然是一个,void*因为那是你声明的那样。任何指针都可以隐式转换为 a void*,因此 cast 什么都不做,您只剩下一个指针,void就像开始时一样。
You'll need to declare it as an int*.
您需要将其声明为int*.
void *some_ptr = /* whatever */;
int *p = (int*)some_ptr;
// now you have a pointer to int cast from a pointer to void
Note that the cast toan int*is also unnecessary, for the same reason you don't have to (and should not) cast the return value of mallocin C.
注意,投给一个int*也是不必要的,出于同样的原因,你不必(也应该不)投的返回值malloc在C.
void*'s can be implicitly converted to and from any other pointer type. I added the cast here only for clarity, in your code you would simply write;
void*'s 可以与任何其他指针类型进行隐式转换。我在这里添加演员表只是为了清楚起见,在您的代码中您只需编写;
int *p = some_void_ptr;
Also, this:
还有这个:
lVptr[1]
Is wrong. You have a pointer to a single int, not two. That dereference causes undefined behavior.
是错的。你有一个指向单个的指针int,而不是两个。这种取消引用会导致未定义的行为。
回答by Cesar Alonso
You can not dereference a voidpointer because it doesn't have a type,
first you need to cast it(int *)lVptr, then dereference it *(int *)lVptr.
您不能取消引用void指针,因为它没有类型,首先您需要对其(int *)lVptr进行强制转换,然后取消引用它*(int *)lVptr。
int lVNum = 2;
void *lVptr;
lVptr = &lVNum;
printf("\nlVptr[60 ] is %d \n",*(int *)lVptr);
回答by Cesar Alonso
A void pointer is just that, a pointer to a void (nothing definable).
void 指针就是指向 void(无法定义)的指针。
Useful in some instances. For example malloc() returns a void pointer precisely because it allocated memory for an UNDEFINED purpose. Some functions may likewise take void pointers as arguments because they don't care about the actual content other than a location.
在某些情况下很有用。例如 malloc() 返回一个空指针,因为它为未定义的目的分配了内存。一些函数可能同样将 void 指针作为参数,因为它们不关心除了位置之外的实际内容。
To be honest, the snippet you posted makes absolutely no sense, can't even guess what you were trying to do.
老实说,您发布的代码段完全没有意义,甚至无法猜测您要做什么。
回答by OregonTrail
Example of what you might be trying to do:
您可能尝试执行的操作示例:
#include <stdio.h>
int main () {
void *v;
unsigned long int *i = (unsigned long int *)v;
*i = 5933016743776703571;
size_t j = sizeof(i);
printf("There are %ld bytes in v\n", j);
size_t k;
for (k = 0; k < j; k++) {
printf("Byte %ld of v: %c\n", k, ((char *)v)[k]);
}
}
Output:
输出:
There are 8 bytes in v
Byte 0 of v: S
Byte 1 of v: T
Byte 2 of v: A
Byte 3 of v: C
Byte 4 of v: K
Byte 5 of v: O
Byte 6 of v: V
Byte 7 of v: R
回答by Saurabh Ghorpade
@ Code-Guru I tried to compile it in visual studio. It gives error - expression must be a pointer to complete object.
@ Code-Guru 我试图在 Visual Studio 中编译它。它给出了错误 - 表达式必须是指向完整对象的指针。
Thanks teppic, As you suggested, the following compiles and yields right result.
谢谢 teppic,正如您所建议的,以下编译并产生正确的结果。
#include<stdio.h>
void main(){
printf("study void pointers \n");
int lvnum = 2;
void *lvptr;
lvptr = &lvnum;
printf("\n lvptr is %d\n",((int *)lvptr)[0]);
}
However if I try printf("\n lvptr is %d\n",((int *)lVptr)[60]); It compiles and runs but gives random number.
但是,如果我尝试 printf("\n lvptr is %d\n",((int *)lVptr)[60]); 它编译并运行,但给出随机数。
Thanks a lot, friends for all the suggestions. Apologies that I assigned a void pointer to unnecessarily casted int pointer and expected it to get dereferenced. However I should have casted it when I want to dereference it.
非常感谢所有朋友的建议。抱歉,我将一个空指针分配给了不必要的 int 指针,并希望它被取消引用。但是,当我想取消引用它时,我应该投射它。
Purpose of the snippet: In my sources I found klocwork error which was caused by similar situation. On the contrary the program not only compiled but also gave correct results. Reason- it is a low level code (no OS) where the memory assigned to the void pointer is already reserved till the count of like 60. But the klocwork tool was unable to parse the files having that limit resulting in error. I did a lot of brain storming and ended up in something silly.
片段的目的:在我的消息来源中,我发现了由类似情况引起的 klocwork 错误。相反,程序不仅编译而且给出了正确的结果。原因 - 这是一个低级代码(没有操作系统),其中分配给 void 指针的内存已经保留到 60 左右。但是 klocwork 工具无法解析具有该限制的文件,从而导致错误。我做了很多头脑风暴,最后得出了一些愚蠢的结论。
Saurabh
索拉布

