我是 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/