简体   繁体   中英

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] .

You're declaring ptr as a void ** but using it as a void * . They're different.

First cast the void pointer array to an array of the pointer type you want. 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) . You're attempting to dereference a void pointer, as your compiler's telling you.

If you want to pass an array of void pointers to func , then you would make the following changes:

  • Change the signature of func to:

    void func(void **ptr)

  • Change func(*ptr); to simply func(ptr); to pass the dynamically allocated array ptr to the function.

I also don't understand why you'd split the declaration and initialisation of ptr in the top snippet into two statements. 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 ). You dereference that pointer with ptr[0] and ptr[1] , trying to extract "somethings". 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. 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). 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. Remove that and you're passing a void** , so that's what the parameter of func should be.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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