gpt4 book ai didi

c++ - 标准转换 : Array-to-pointer conversion

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:12:07 25 4
gpt4 key购买 nike

这是来自 ISO 的要点:标准转换:数组到指针的转换:$4.2.1

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.

谁能解释一下,如果可能的话,用一个示例程序。

我已经看过这些链接,但我无法理解:

Array and Rvalue

I think I may have come up with an example of rvalue of array type

最佳答案

在 C 和 C++ 中,数组可以用作指向其第一个元素的指针。实际上,给定一个名为 x 的数组,您可以将 &x[0] 的大部分使用仅替换为 x

这是下标如何与数组对象一起使用:

int x[5];
x[2]; // this is the same as (&x[0])[2]

这也是将数组传递给具有指针类型参数的函数的方式:

void f(int* p);

int x[5];
f(x); // this is the same as f(&x[0])

有几种情况不会发生数组到指针的转换。示例包括当数组是 sizeof 或一元 &(地址运算符)的操作数时,当字符串文字用于初始化数组时,以及当数组绑定(bind)到对数组的引用。

关于c++ - 标准转换 : Array-to-pointer conversion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4223617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com