开发者

Casting pointer warning

开发者 https://www.devze.com 2023-03-01 02:43 出处:网络
In a C program I am writing, there is a function void *dereference_physical_page(unsigned int ppn);

In a C program I am writing, there is a function void *dereference_physical_page(unsigned int ppn);

which I call with unsigned int* pde = ((unsigned int*)dereference_physical_page(context >> 12))[vaddr >> 22];

Unfortunately the compil开发者_开发知识库er gives me that warning no matter what I try, and the program needs to have no warnings in order to compile, according to the specifications for this assignment.

edit (I thought I had posted this): The warning is "initialization makes pointer from integer without a cast". If i remove the asterisk of unsigned int* pde, this works; however, I want to make pde a pointer.

I am also making pde a pointer because I need its scope to extend beyond that of the function it's in.

Any clues?


You are dereferencing the pointer with [vaddr >> 22], which means the expression has a type of unsigned int. Then you're assigning that to an unsigned int *. That's where the error is coming from.


If you just want a pointer to the element you'd have to use +(vaddr >> 22) instead of [vaddr >> 22]. Please have in mind that pointer arithmetic here counts in sizes of unsigned and not in bytes.

But frankly, you don't even seem to be well aware of how pointers work in C. You definitively shouldn't use such hacks before you master these things better.


You didn't say what "that warning" is, but you are indexing an unsigned int*, which yields an unsigned int, and then trying to assign it to an unsigned int*, which will rightfully give you a warning. If you really need the result to be a pointer, then cast the value from dereference_physical_page to unsigned int** (two '*'s instead of one). That presumes that the result of dereference_physical_page(context >> 12) is the address of an array of unsigned int*s and you want the (vaddr >> 22)th one.


((unsigned int*)dereference_physical_page(context >> 12))[vaddr >> 22];
You are using the type casted pointer returned by dereference_physical_page and accessing an array element using this returned pointer...

0

精彩评论

暂无评论...
验证码 换一张
取 消