- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我已经阅读了很多关于指针和二维数组关系的帖子,但我似乎无法理解概念。假设有一个二维数组 int a[3][2]
和一个数组 int b[2]
。
现在 a
返回一个指向大小为 3 的整数数组的指针。它将是 int (*)[2]
类型。
随着我对这个概念的理解,取消引用它 (*a
) 会给我数组本身,这会衰减到指向一维数组第一个元素的指针,并且是类型(int*) 。现在大胆的部分是怀疑。
为什么和如何这种衰减发生在数组本身(这是完整的一维数组 a[0])衰减到一维数组的第一个元素的地方?
(似乎无法理解为什么和如何部分) 并且似乎也无法在其他链接上找到它。
和a[0]
一样,*a
,&a[0][0]
代表同一个指针。这里 a[0] 是第一个 1d 数组。那么 b
(上面声明的)和 a[0]
是否代表相同的东西(都是一维数组,然后衰减到指向数组中第一个元素的指针)输入(整数*)?
最佳答案
why and how does this decay happen where the array itself decays to first element of the 1d array?
Except when it is the operand of the
sizeof
operator, the_Alignof
operator 1,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.
简单;数组名称可以转换为指向其第一个元素的指针。
由于 a
是一个数组名,它会衰减到指向其第一个元素的指针,该元素是一维数组。它的类型是 int(*)[2]
。因为a
(衰减后)是一个指向2
int
数组的指针,所以*a
是一个数组2
int
。由于这是一个数组类型,即 *a
是第一个一维数组的数组名称,它会衰减为指向第一个元素的指针 (a[0] [0]
) 数组对象。所以,它的类型是int *
。
is this true that
b
(declared above) anda[0]
represent the same thing (both being 1d array and then decay to the pointer to first element in array of type (int*
)?
是的 b
和 a[0]
都是 int *
类型(衰减后)。
<子>1。阅读 Keith Thompson 的通信.另请阅读此 answer其中指出:C11 标准的草案指出数组存在另一个异常(exception)情况,即当数组是新的 _Alignof
运算符的操作数时。这是草案中的一个错误,在最终发布的 C11 标准中得到更正; _Alignof
只能应用于带括号的类型名称,不能应用于表达式。
关于c - 为什么取消引用指向整数数组(在二维数组中)的指针会返回(或衰减到)指向第一个元素的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24121010/
liwen01 2024.08.18 前言 无论是在产品开发还是在日常生活中,在使用无线网络的时候,都会经常遇到一些信号不好的问题,也会产生不少疑问: 为什么我们在高速移动的高铁上网络会变
我正在使用 Kinect 获取每个关节的位置和方向,然后将它们发送到 Unity。我注意到值有很多“跳跃”或波动,例如,有时我不移动我的手,而在 Unity 中它会旋转 180 度。 我想要的是一个平
在下面的示例中, #include #include //okay: // template decltype(auto) runner(T&& t, F f) { return f(st
出于某种原因,即使我设置了衰减因子,我的学习率似乎也没有改变。我添加了一个回调来查看学习率,它似乎在每个纪元之后都是一样的。为什么没有变化 class LearningRatePrinter(Call
考虑下面的代码: #include #include using namespace std; template void Test2(future f, Work w) { async([
我是一名优秀的程序员,十分优秀!