指针是否支持“数组样式索引"? [英] Do pointers support "array style indexing"?

查看:139
本文介绍了指针是否支持“数组样式索引"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(回答常见问题解答-这件事不断弹出")

我认为读者已经知道指针算术的工作原理.

I assume that the reader is aware of how pointer arithmetic works.

int arr[3] = {1,2,3};
int* ptr = arr;
...
*(ptr + i) = value;

老师/C书籍不断告诉我,我不应该像上面的示例中那样使用*(ptr + i),因为指针支持数组样式索引"会被引用.我应该改用ptr[i] = value;.那里没有论据-更容易阅读.

Teachers/C books keep telling me I shouldn't use *(ptr + i) like in the above example, because "pointers support array style indexing" and I should be using ptr[i] = value; instead. No argument there - much easier to read.

但是,从C标准来看,我什么都没有找到,称为数组样式索引".实际上,运算符[]并不期望任何一个操作数都是数组,而是指针或整数!

But looking through the C standard, I find nothing called "array style indexing". In fact, the operator [] is not expecting either operand to be an array, but instead a pointer or an integer!

6.5.2.1数组下标

约束

其中一个表达式的类型应为完成对象 type 的指针",另一个表达式的类型应为整数",结果的类型应为" type ". ".

One of the expressions shall have type ‘‘pointer to complete object type’’, the other expression shall have integer type, and the result has type ‘‘type’’.

为什么 array 下标运算符不期望数组?标准错了吗?我的老师/C书是否感到困惑?

Why does the array subscripting operator not expect an array? Is the standard wrong? Is my teacher/C book confused?

推荐答案

出于可读性原因,您确实应该在*(ptr + i)上使用ptr[i].但是除此之外,严格来讲,[]运算符实际上从未与数组操作数一起使用.

You should indeed be using ptr[i] over *(ptr + i) for readability reasons. But apart from that, the [] operator is, strictly speaking, actually never used with an array operand.

数组在表达式中使用时,总是衰减"到指向第一个元素的指针中(某些例外). C17 6.3.2.1/3,重点是:

Arrays, when used in an expression, always "decay" into a pointer to the first element (with some exceptions). C17 6.3.2.1/3, emphasis mine:

除非它是sizeof运算符的操作数或一元&运算符,或者是用于初始化数组的字符串文字,类型为"array of type"的表达式将转换为类型为"pointer to type"的表达式,该表达式指向该数组的初始元素数组对象,它不是左值.

意味着,只要您键入arr[i],操作数arr就会被指向该数组内第一个元素的指针所代替.这被非正式地称为阵列衰减".此处的更多信息:什么是数组衰减?

Meaning that whenever you type arr[i], the operand arr gets replaced by a pointer to the first element inside that array. This is informally referred to as "array decaying". More info here: What is array decaying?

因此,每当使用[]运算符时,都将其用于指针.总是.

So whenever you use the [] operator, you use it on a pointer. Always.

C标准指出,保证该运算符等效于指针算术(C17 6.5.2.1/2):

The C standard says that this operator is guaranteed to be equivalent to the pointer arithmetic (C17 6.5.2.1/2):

下标运算符[]的定义是E1[E2](*((E1)+(E2)))相同.

The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))).

因此,每当我们键入arr[i]时,它实际上就会被*(arr+i)静默替换. arr仍然是第一个元素的指针.

So whenever we type arr[i], it actually gets silently replaced by *(arr+i). Where arr is still a pointer to the first element.

这就是为什么您引用的描述告诉您两个操作数可以是指针,而另一个是整数.因为显然我们键入*(arr+i)*(i+arr)并不重要-这是等效的代码.

And this is why the description you quoted tells you that either operand could be a pointer and the other an integer. Because obviously it doesn't matter if we type *(arr+i) or *(i+arr) - that's equivalent code.

反过来,这又允许我们编写模糊的笑话"代码,例如i[arr],该代码实际上是有效的C,并且完全等效于arr[i].但是不要在实际的应用程序中编写此类代码.

Which in turn allows us to write obfuscated "joke" code like i[arr], which is actually valid C and fully equivalent to arr[i]. But don't write such code in real applications.

这篇关于指针是否支持“数组样式索引"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆