gcc 对什么是左值感到困惑?

发布于 2024-10-06 14:47:12 字数 246 浏览 0 评论 0原文

gcc 在赋值错误中给了我无效的左值:

-2[(size_t *)new] = 0;

将代码更改为以下内容使其消失:

((size_t *)new)[-2] = 0;

但据我所知,两者在 C 中都是 100% 等效。gcc 在非左值表达式中使用前者没有问题。这只是 gcc 中的一个错误吗?我已经用多个版本对其进行了测试,并得到了相同的结果。

gcc is giving me invalid lvalue in assignment errors for:

-2[(size_t *)new] = 0;

Changing the code to the following makes it go away:

((size_t *)new)[-2] = 0;

but as far as I can tell, both are 100% equivalent in C. gcc has no problem using the former in non-lvalue expressions. Is this just a bug in gcc? I've tested it with several versions and got the same results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

围归者 2024-10-13 14:47:12

我很傻。 []- 绑定更紧密,因此该表达式实际上读取索引 2 并将其取反。

I'm stupid. [] binds more tightly than -, so this expression is actually reading index 2 and negating it.

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