为什么这个片段可以用 C 编译?

发布于 2024-08-05 11:50:17 字数 213 浏览 7 评论 0原文

可能的重复:
在 C 数组中为什么会这样? a[5] == 5[a]

3["zdvnngfgnfg"];

Possible Duplicate:
In C arrays why is this true? a[5] == 5[a]

3["zdvnngfgnfg"];

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

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

发布评论

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

评论(3

找回味觉 2024-08-12 11:50:17

它相当于

"zdvnngfgnfg"[3];

合法的,意思是“获取该文字的地址并向其添加 3*sizeof(char)”。反正不会有什么影响。

另请参阅这个非常相似的问题。

It's equivalent to

"zdvnngfgnfg"[3];

which is legal and means "take the address of that literal and add 3*sizeof(char) to it". Will have no effect anyway.

Also see this very similar question.

懒的傷心 2024-08-12 11:50:17

arr[i] 被解析为 *(arr+i) ,可以写为 *(i+arr) ,因此 i[arr]
现在“strngjwdgd”是一个指向存储在只读位置的常量字符数组的指针。
所以它有效!

arr[i] is parsed as *(arr+i) which can be written as *(i+arr) and hence i[arr]
Now "strngjwdgd" is a pointer to a constant character array stored at read only location.
so it works!!

¢蛋碎的人ぎ生 2024-08-12 11:50:17

字符串文字(array) 衰减为char* 类型的指针。然后取第四个元素:

3["zdvnngfgnfg"] == "zdvnngfgnfg"[3]

为什么可以在数组前面写下标又是一个问题:

在 C 数组中为什么会这样?

The string literal(array) decays to a pointer of type char*. Then you take the fourth element:

3["zdvnngfgnfg"] == "zdvnngfgnfg"[3]

Why you can write the subscript infront of the array is another question:

In C arrays why is this true?

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