c - 在 C 中访问数组元素的不同方式

标签 c arrays

我是 C 编程类(class)的助教,遇到了以下 C 代码行:

char str[] = "My cat's name is Wiggles.";
printf("%c %c %c %c\n", str[5], *(str + 5), *(5 + str), 5[str]);

我之前从未遇到过最后一个参数 (5[str]),我的教授也没有遇到过。我认为 K&R 和 C Primer Plus 中没有提到它。我在一组技术面试题中找到了这段代码。有谁知道为什么 C 也允许您以这种方式访问​​数组元素?我从没听说过索引在括号外,数组名在括号内。

非常感谢您的帮助!

最佳答案

完全有效 C. 来自 Wikipedia :

Similarly, since the expression a[i] is semantically equivalent to *(a+i), which in turn is equivalent to *(i+a), the expression can also be written as i[a] (although this form is rarely used).

古怪,但有效。

关于c - 在 C 中访问数组元素的不同方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5546075/

相关文章:

C 从文件中读取多个数据类型

Postgresql 中的 C 库函数

android - 我可以通过数组定义多个 LinearLayouts 吗?

ios - 为什么当我尝试附加数组时应用程序崩溃?

javascript - 如何从数组中删除偶数?

c - 字符串数组正确扫描,然后一起运行

c++ - 如何在 Windows 上链接动态构建的 cmake 文件?

javascript - .reduce() 函数如何与 Javascript 中的 Math.max() 一起使用?

c - Linux C中的段错误

php - Codeigniter 将查询加载到数组中