为什么数组名称指向数组的第一个元素? [英] why is array name a pointer to the first element of the array?

查看:434
本文介绍了为什么数组名称指向数组的第一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时总是这样的情况下,我的意思是,数组名始终是一个指针array.why的第一个元素会这样呢?这是实施的东西有点事或者语言功能?

Is this always the case , i mean , that array name is always a pointer to the first element of the array.why is it so?is it something implementation kinda thing or a language feature?

推荐答案

这是数组名本身不是一个指针,但衰变成一个指针,在大多数情况下数组的第一个元素。这是这样,因为语言定义了这种方式。

An array name is not itself a pointer, but decays into a pointer to the first element of the array in most contexts. It's that way because the language defines it that way.

从C11的 6.3.2.1左值,数组和功能代号,第3段:

除非它是在 的sizeof 的操作数运营商,在 _Alignof 运营商,或一元的 &安培; 运营商,或者是用于初始化数组文本字符串,当然$ p有类型转换为一个前pression,类型为指针的键入的指向数组对象的初始元素的键入的数组$ pssion而不是左值。

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue.

您可以了解更多有关这个话题(和许多所涉及的微妙的行为)从阵列和指针部分 comp.lang.c常见问题解答

You can learn more about this topic (and lots about the subtle behaviour involved) from the Arrays and Pointers section of the comp.lang.c FAQ.

编辑旁白:同一种行为发生在C ++中,虽然语言指定它有点不同。作为参考,从C ++ 11草案我这里有, 4.2阵列到指针转换第1款规定:

Editorial aside: The same kind of behaviour takes place in C++, though the language specifies it a bit differently. For reference, from a C++11 draft I have here, 4.2 Array-to-pointer conversion, paragraph 1:

这是左值或类型的右值的 N T阵列或未知的数组边界<$的C $ C> T 可转换为类型的右值指针 T 。其结果是一个指针数组的第一元素

An lvalue or rvalue of type "array of N T" or "array of unknown bound of T" can be converted to an rvalue of type "pointer to T". The result is a pointer to the first element of the array.

这篇关于为什么数组名称指向数组的第一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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