简体   繁体   English

C尝试将void指针强制转换为另一种类型

[英]C trying to cast a void pointer to another type

Alright here's the code: 好的,这是代码:

//in another file

void **ptr; ptr = kmalloc(sizeof(void *) * 2);

  *(ptr+0) = tf; //type trapframe *
  *(ptr+1) = as; //type addrspace *

func(*ptr); 

And here is that function: 这是该函数:

void func(void *ptr) {

struct trapframe *parentTF = ptr[0];
struct addrspace *newAS = ptr[1]; 
//now I wanna do stuff with parentTF and newAS

}

And the error I get is: 我得到的错误是:

warning: dereferencing `void *' pointer

Thanks for any help. 谢谢你的帮助。

If I'm correctly understanding what you're trying to do, it seems like you need to change this: 如果我正确理解了您要执行的操作,则似乎需要更改以下内容:

void func(void *ptr) {

to this: 对此:

void func(void **ptr) {

and this: 和这个:

func(*ptr);

to this: 对此:

func(ptr);

Note that *(ptr+0) and ptr[0] are synonymous, as are *(ptr+1) and ptr[1] . 注意, *(ptr+0)ptr[0]是同义的, *(ptr+1)ptr[1]也是同义的。

You're declaring ptr as a void ** but using it as a void * . 您将ptr声明为void **但将其用作void * They're different. 他们不同。

First cast the void pointer array to an array of the pointer type you want. 首先将void指针数组转换为所需指针类型的数组。 ie, you need to do such changes: 即,您需要进行以下更改:

((trapframe **)ptr)[0] = tf; //type trapframe *

and another cast like this: 另一个像这样的演员:

struct trapframe *parentTF = ((trapfname**)ptr)[0];

In func , where ptr is declared as a void* , ptr[0] is the same as *ptr and ptr[1] is the same as *(ptr + 1) . func ,将ptr声明为void*ptr[0]*ptr相同,而ptr[1]*(ptr + 1) You're attempting to dereference a void pointer, as your compiler's telling you. 正如编译器所告诉的那样,您正在尝试取消引用void指针。

If you want to pass an array of void pointers to func , then you would make the following changes: 如果要将void指针数组传递给func ,则需要进行以下更改:

  • Change the signature of func to: func的签名更改为:

    void func(void **ptr)

  • Change func(*ptr); 更改func(*ptr); to simply func(ptr); 简单地func(ptr); to pass the dynamically allocated array ptr to the function. 将动态分配的数组ptr传递给函数。

I also don't understand why you'd split the declaration and initialisation of ptr in the top snippet into two statements. 我也不明白为什么您要将顶部代码段中ptr的声明和初始化分成两个语句。 Just have: 只需:

void **ptr = kmalloc(sizeof(void *) * 2);

or even: 甚至:

void **ptr = kmalloc(sizeof(*ptr) * 2);

And what did you expect? 你期望什么? The function's parameter ptr is a pointer to "something" (ie void ). 该函数的参数ptr是指向“某物”(即void )的指针。 You dereference that pointer with ptr[0] and ptr[1] , trying to extract "somethings". 您可以使用ptr[0]ptr[1]取消引用该指针,以尝试提取“内容”。 But the compiler doesn't know the type or size of "something". 但是编译器不知道“东西”的类型或大小。

What you probably want is this: 您可能想要的是:

func(ptr);

and this: 和这个:

void func(void** ptr)
{
...
}

Your code screams wrongness because of the inconsistencies between the two files. 由于两个文件之间的不一致,您的代码会发出错误提示。 In one, you access ptr[0] and ptr[1], while in the other you access *(ptr + 0) and *(ptr + 1) ... that happens not to be a source of error here because the two syntaxes mean the same thing, but using two different forms is bad style, reads badly, and is error prone. 在其中一个中,您访问ptr [0]和ptr [1],而在另一个中,您访问*(ptr + 0)和*(ptr + 1)...在这里碰巧不是错误源,因为两者语法具有相同的含义,但是使用两种不同的形式是不好的风格,不好读并且容易出错。 But then, in one file you declare void **ptr but in the other file you declare void *ptr -- that can't possibly be be right, since the two ptrs have the same semantics (they each point to an array of two elements, a tf and an as). 但是然后,在一个文件中,您声明void **ptr但在另一个文件中,声明void *ptr可能不正确,因为两个void *ptr具有相同的语义(它们每个都指向两个数组元素,tf和as)。 In one file you have a function that takes a parameter called ptr, but in the other file you pass the contents of a variable named ptr ... again, since the two ptrs have the same semantics, this inconsistency must be wrong, and clearly it's the dereference that is wrong. 在一个文件中,您有一个函数带有一个名为ptr的参数,但是在另一个文件中,您又传递了一个名为ptr的变量的内容 ...,因为两个ptr具有相同的语义,所以这种不一致肯定是错误的,而且很明显取消引用是错误的。 Remove that and you're passing a void** , so that's what the parameter of func should be. 删除它,然后传递void** ,这就是func的参数。

Code consistently and a whole class of errors will disappear from your code. 一致地编写代码,一整套错误将从代码中消失。 You can code for 3 years or for 30 years, but it doesn't matter if you don't learn such fundamentals. 您可以编写3年或30年的代码,但是,如果您不了解这些基础知识,也没关系。

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

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