铸造指针警告
在我编写的C程序中,有一个函数 void *dereference_physical_page(unsigned int ppn);
我用 unsigned int* pde = ((unsigned int*)dereference_physical_page(context >> 12))[vaddr >> 22]; 不幸的是,
无论我尝试什么,编译器都会给我这个警告,并且根据此作业的规范,程序不需要任何警告即可编译。
编辑(我以为我已经发布了这个):警告是“初始化使指针来自整数而不进行强制转换”。如果我删除 unsigned int* pde 的星号,则此方法有效;但是,我想让 pde 成为一个指针。
我还将 pde 设为指针,因为我需要将其范围扩展到其所在函数的范围之外。
有任何线索吗?
In a C program I am writing, there is a functionvoid *dereference_physical_page(unsigned int ppn);
which I call with unsigned int* pde = ((unsigned int*)dereference_physical_page(context >> 12))[vaddr >> 22];
Unfortunately the compiler 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在使用
[vaddr >>> 取消引用指针22]
,这意味着表达式的类型为unsigned int
。然后将其分配给一个unsigned int *
。这就是错误的来源。You are dereferencing the pointer with
[vaddr >> 22]
, which means the expression has a type ofunsigned int
. Then you're assigning that to anunsigned int *
. That's where the error is coming from.如果您只想要指向元素的指针,则必须使用
+(vaddr >> 22)
而不是[vaddr >> 22]
。请记住,这里的指针算术以无符号大小而不是字节为单位进行计数。但坦率地说,您似乎甚至不太了解指针在 C 中的工作原理。在更好地掌握这些东西之前,您绝对不应该使用此类技巧。
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 ofunsigned
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.
您没有说明“该警告”是什么,但您正在索引一个
unsigned int*
,它会产生一个unsigned int
,然后尝试将其分配给 < code>unsigned int*,这将正确地向您发出警告。如果您确实需要结果为指针,请将值从dereference_physical_page
转换为unsigned int**
(两个“*”而不是一个)。假设dereference_physical_page(context >> 12)
的结果是unsigned int*
数组的地址,并且您想要(vaddr > ;>22) 第一个。
You didn't say what "that warning" is, but you are indexing an
unsigned int*
, which yields anunsigned int
, and then trying to assign it to anunsigned int*
, which will rightfully give you a warning. If you really need the result to be a pointer, then cast the value fromdereference_physical_page
tounsigned int**
(two '*'s instead of one). That presumes that the result ofdereference_physical_page(context >> 12)
is the address of an array ofunsigned int*
s and you want the(vaddr >> 22)
th one.((unsigned int*)dereference_physical_page(context>>12))[vaddr>> 22];
您正在使用
dereference_physical_page
返回的类型转换指针并使用此返回的指针访问数组元素...((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...