是否括号[]运营商只有一个单一的用? [英] Does the bracket [] operator only have a single use?

查看:138
本文介绍了是否括号[]运营商只有一个单一的用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初以为它有指针和数组不同的用途。在前者的情况下,它增加了无论是在括号的指针,然后取消引用的总和;在后一种情况下,将刚刚得到的数组的第i个元素。

I initially thought that it has a different use for pointers and for arrays. In the former case, it adds whatever is in brackets to the pointer and then dereferences the sum; in the latter case it would just yield the ith element of an array.

然后我意识到一个数组变量返回指针的第一要素,因此运营商确实在每种情况下同样的事情:偏移和反引用

Then I realized that an array variable returns the pointer to the first element, so the operator does the same thing in each case: offset and dereference.

是否括号[]运营商确实只有在C一次性使用?

Does the bracket [] operator indeed only have a single use in C?

推荐答案

[] 被称为数组下标运算符,但在语法上它是一个指针使用。阵列被转换为指针的第一个元素在此使用(和许多其他的)。所以,是的, [] 是数组和指针一样的。

[] is called array subscript operator, but syntactically it's used on a pointer. An array is converted to a pointer to the first element in this usage (and many others). So, yes, [] is the same for arrays and pointers.

限制

其中前pressions应有类型'指针来完成对象类型',其他
  前pression应具有整数类型,结果的类型''类型''。

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’’.

语义

一个后缀前pression随后在方括号中前pression [] 是一个下标
  阵列对象的元素的指定。下标运算符的定义 []
  是 E1 [E2] (*((E1)+(E2)))。由于转换规则的那
  适用于二进制 + 运营商,如果 E1 是一个数组对象(等同于一个指针的初始元素数组对象)和 E2 是一个整数, E1 [E2] 指定的 E2 >(从零算起)。 E1

A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).

这篇关于是否括号[]运营商只有一个单一的用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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