C 指针:它们具有相同的功能吗?

发布于 2024-12-10 18:16:09 字数 317 浏览 1 评论 0原文

假设您有一个“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 技术交流群。

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

发布评论

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

评论(1

弃爱 2024-12-17 18:16:09

这些并不等同。 #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 be unsigned.

a is a void pointer, so it can be casted to *unsigned, but not to unsigned (implicitly), that's why you get the warning.

The #3 resolves the same to a char, which would also yield a warning.

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