C 指针:它们具有相同的功能吗?
假设您有一个“void *a”和“void *ptr”指向代码中定义的不同地址。然后我想知道这两行是否等效且功能相同?
*((unsigned **)((char*)ptr+4)) = a;
第二
*((unsigned *)((char*)ptr+4)) = a;
个抛出警告“赋值从指针生成整数而无需强制转换”
此外,它是否也与上面的操作相同?:
*((char*)ptr+4) = a;
Let's say you have a "void *a" and "void *ptr" that point to different addresses defined in your code. Then I was wondering if these two lines were equivalent and functionally the same?
*((unsigned **)((char*)ptr+4)) = a;
and
*((unsigned *)((char*)ptr+4)) = a;
The second one throws a warning that "assignment makes integer from pointer without a cast"
Also, would it also be the same as the above to just do?:
*((char*)ptr+4) = a;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些并不等同。 #1 将
(char*)ptr+4
解析为指向无符号 (*unsigned
) 的指针,而 #2 将其解析为unsigned
。a
是一个 void 指针,因此它可以转换为*unsigned
,但不能转换为unsigned
(隐式),这就是您收到警告的原因。#3 将其解析为
char
,这也会产生警告。These are not equivalent. #1 resolves
(char*)ptr+4
to be a pointer to unsigned (*unsigned
), while #2 resolves it to beunsigned
.a
is a void pointer, so it can be casted to*unsigned
, but not tounsigned
(implicitly), that's why you get the warning.The #3 resolves the same to a
char
, which would also yield a warning.