标准转换:数组到指针的转换 [英] standard conversions: Array-to-pointer conversion
问题描述
这是 ISO :Standard Conversions:Array-to-pointer conversion: $4.2.1
数组"类型的左值或右值N T"或未知边界的数组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:
推荐答案
在 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屋!