标准转换:数组到指针的转换 [英] standard conversions: Array-to-pointer conversion
问题描述
这是从ISO的点:标准转换:数组到指针的转换:$ 4.2.1 的
这是左值或类型的右值数组
NT·的或未知的数组边界的
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.
可以的,如果可能的一个例子程序,任何一个解释。
Can any one explain this, if possible with an example program.
我看到这些链接了,但我无法理解:
I seen these links already, but i am unable to understand:
<一个href=\"http://stackoverflow.com/questions/4058151/i-think-i-may-have-come-up-with-an-example-of-rvalue-of-array-type\">I想我可能已经想出了数组类型的右值的一个例子
推荐答案
在C和C ++编写的阵列可以就好像它是一个指向第一个元素被使用。实际上,鉴于命名的数组 X
,可以替换&放的大多数用途; X [0]
只有 X
。
In both C and C++, an array can be used as if it were a pointer to its first element. Effectively, given an array named x
, you can replace most uses of &x[0]
with just x
.
这是下标是如何能够用数组对象使用:
This is how subscripting is able to be used with array objects:
int x[5];
x[2]; // this is the same as (&x[0])[2]
这也是一个阵列可以如何被传递到具有指针类型的参数的函数:
This is also how an array can be passed to a function that has a parameter of pointer type:
void f(int* p);
int x[5];
f(x); // this is the same as f(&x[0])
有若干上下文,其中阵列到指针转换不会发生。例子包括:当一个数组是中的sizeof
操作数或一元 - &安培;
(经营地址的),当一个字符串文字是用来初始化数组,当一个数组被绑定到一个数组的引用。
There are several contexts in which the array-to-pointer conversion does not take place. Examples include when an array is the operand of sizeof
or the unary-&
(the address-of operator), when a string literal is used to initialize an array, and when an array is bound to a reference to an array.
这篇关于标准转换:数组到指针的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!