简体   繁体   English

取消引用无效指针

[英]Dereference void pointer

Even after casting a void pointer, I am getting compilation error while dereferencing it. 即使在转换了一个void指针之后,我在解除引用时也遇到了编译错误。 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]);

It doesn't make sense to dereference a void pointer. 取消引用void指针没有意义。 How will the compiler interpret the memory that the pointer is pointing to? 编译器如何解释指针指向的内存? You need to cast the pointer to a proper type first: 您需要先将指针强制转换为正确的类型:

int x = *(int*)lVptr;

printf("\\nlVptr[60 ] is %d \\n", *(int*)lVptr);

This will cast the void pointer to a pointer to an int and then dereference it correctly. 这会将void指针强制转换为指向int的指针,然后正确地取消引用它。

If you want to treat it as an array (of one), you could do a slightly ugly ((int *)lVptr)[0] . 如果你想把它当作一个数组(一个),你可以做一个稍微丑陋的((int *)lVptr)[0] Using [1] is out of bounds, and therefore not a good idea (as for lVptr[60] ...) 使用[1]是出界的,因此不是一个好主意(对于lVptr[60] ......)

It's still a void* because that's what you declared it as. 它仍然是一个void*因为这就是你宣称的那样。 Any pointer may be implicitly converted to a void* , so that cast does nothing and you are left with a pointer to void just as you began with. 任何指针都可以隐式转换为void* ,这样转换就不会执行任何操作,并且在开始时就会留下指向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 to an int* is also unnecessary, for the same reason you don't have to (and should not ) cast the return value of malloc in C. 需要注意的是强制转换 int*也是不必要的,因为你没有同样的原因( 也不应该)投的返回值malloc在C.

void* 's can be implicitly converted to and from any other pointer type. void*可以隐式转换为任何其他指针类型。 I added the cast here only for clarity, in your code you would simply write; 为了清楚起见,我在这里添加了演员,在你的代码中你只需写;

int *p = some_void_ptr;

Also, this: 这个:

lVptr[1]

Is wrong. 是错的。 You have a pointer to a single int , not two. 你有一个指向单个int的指针,而不是两个指针。 That dereference causes undefined behavior. 该取消引用会导致未定义的行为。

You can not dereference a void pointer because it doesn't have a type, first you need to cast it (int *)lVptr , then dereference it *(int *)lVptr . (int *)lVptr引用一个void指针,因为它没有一个类型,首先需要将它转换为(int *)lVptr ,然后取消引用它*(int *)lVptr

int lVNum = 2;
void *lVptr;
lVptr = &lVNum;

printf("\nlVptr[60 ] is  %d \n",*(int *)lVptr);

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. 例如,malloc()精确地返回一个void指针,因为它为UNDEFINED目的分配了内存。 Some functions may likewise take void pointers as arguments because they don't care about the actual content other than a location. 某些函数同样可以将void指针作为参数,因为它们不关心位置以外的实际内容。

To be honest, the snippet you posted makes absolutely no sense, can't even guess what you were trying to do. 说实话,你发布的片段绝对没有意义,甚至无法猜出你想要做什么。

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

@ Code-Guru I tried to compile it in visual studio. @ Code-Guru我试图在visual studio中编译它。 It gives error - expression must be a pointer to complete object. 它给出了错误 - 表达式必须是指向完整对象的指针。

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]); 但是,如果我尝试printf(“\\ n lvptr is%d \\ n”,((int *)lVptr)[60]); It compiles and runs but gives random number. 它编译并运行,但给出随机数。

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. 抱歉,我指定了一个void指针,指向不必要的int指针并期望它被解除引用。 However I should have casted it when I want to dereference it. 但是,当我想要取消引用它时,我应该将它转换为它。

Purpose of the snippet: In my sources I found klocwork error which was caused by similar situation. 片段的目的:在我的来源中,我发现klocwork错误是由类似情况引起的。 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. 原因 - 它是一个低级代码(无操作系统),其中分配给void指针的内存已经被保留,直到计数为60.但是klocwork工具无法解析具有该限制的文件导致错误。 I did a lot of brain storming and ended up in something silly. 我做了大量的脑力激荡,最终陷入了愚蠢的境地。

Saurabh SAURABH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM