铸造指针警告

发布于 2024-11-03 09:24:55 字数 441 浏览 1 评论 0原文

在我编写的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 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 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过潦 2024-11-10 09:24:55

您正在使用 [vaddr >>> 取消引用指针22],这意味着表达式的类型为unsigned int。然后将其分配给一个unsigned int *。这就是错误的来源。

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.

醉酒的小男人 2024-11-10 09:24:55

如果您只想要指向元素的指针,则必须使用 +(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 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.

╭⌒浅淡时光〆 2024-11-10 09:24:55

您没有说明“该警告”是什么,但您正在索引一个 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 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.

残龙傲雪 2024-11-10 09:24:55

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文