“赋值使指针无需强制转换”是什么意思?意思是?
早些时候,我问过类似的问题,但后来我更改了代码。现在编译器给了我一个不同的警告。这是我的代码现在的示例:
void *a = NULL;
void *b = //something;
a = *(int *)((char *)b + 4);
当我尝试编译时,我收到“警告:赋值使指针来自整数而不进行强制转换。”这是什么意思?我应该做什么来解决它?
澄清一下,我不希望“a”指向比“b”大4个字节的地址(即a!= b+4)。在我的程序中,我知道存储在 ((char *)b + 4) 处的值本身就是另一个指针,我想将该指针存储在“a”中。
Earlier, I asked a similar question, but I've since changed my code. Now the compiler gives me a different warning. This is an example of what my code looks like now:
void *a = NULL;
void *b = //something;
a = *(int *)((char *)b + 4);
When I try to compile, I get "warning: assignment makes pointer from integer without a cast." What does this mean, and what should I do to fix it?
To clarify, I don't want 'a' to point to an address that is 4 bytes greater than 'b' (i.e., a != b+4). In my program, I know that the value stored at ((char *)b + 4) is itself another pointer, and I want to store this pointer in 'a'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为,“存储在
((char *)b + 4)
的值本身就是另一个指针”,只需向结果添加显式转换:因为您假设
sizeof(void *)==sizeof(int)
,代码不可移植,但我相信您已经知道了。一种可移植的替代方法是使用
intptr_t
而不是int
。Since, "the value stored at
((char *)b + 4)
is itself another pointer", simply add an explicit cast to the result:Since you're assuming that
sizeof(void*)==sizeof(int)
, the code is not portable, but I am sure you already knew that.A portable alternative is to use
intptr_t
instead ofint
.那么
b+4
中存储了一个指针值,您想将其加载到a
中吗?然后将其视为 1,因此 (b+4) 指向一个指针 - 指向 int 指针的指针是int**
:So there's a pointer value stored at
b+4
which you want to load intoa
? Then treat it as one, and so (b+4) points to a pointer - pointer to a pointer to int isint**
.: