指向指针衰减的数组是否已更改为指针对象? [英] Is the array to pointer decay changed to a pointer object?

查看:50
本文介绍了指向指针衰减的数组是否已更改为指针对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int a[] = {1, 2 ,3};

我知道数组名称会转换为指针。

I understand that array names are converted to pointers. A term often used is that they decay to pointers.

对我来说, pointer 是一个内存区域

However to me, a pointer is a region of memory that holds the address to another region of memory, so:

int *p = a;

可以这样绘制:

-----              -----
  p    --------->  a[0].  .....
-----              -----
 0x1                0x9

但是 a 本身并不指向内存的另一个区域,而是内存本身的区域。
那么,当编译器将其转换为指针时,是否将其保存(例如 p )在内存中或
的隐式转换中? p>

But a itself is not pointing to another region of memory, it IS the region of memory itself. So when the compiler converts it to a pointer, does it save it (like p) somewhere in memory or it's an implicit conversion?

推荐答案


"但是 a 本身不是

,因此,当编译器将其转换为指针时,它是否将其保存(如 p )在内存中的某个地方还是隐式转换?

"So when the compiler converts it to a pointer, does it save it (like p) somewhere in memory or it's an implicit conversion?"

这是隐式转换。编译器不会实现在内存中创建单独的指针对象(您可以用任何方式分配该指针对象以不同的内存地址)来保存第一个元素的地址。

It is an implicit conversion. The compiler does not implement the creation of a separate pointer object in memory (which you can f.e. assign in any manner with a different memory address) to hold the address of the first element.

标准状态(强调我的状态):

The standard states (emphasize mine):


,除非它是sizeof运算符的操作数或一元&运算符,或者是用于初始化数组的字符串文字,该表达式的类型为 array of type。将其转换为类型为 pointer to type的表达式。指向数组对象的初始元素,不是左值。如果数组对象具有寄存器存储类,则该行为未定义。

"Except when it is the operand of the sizeof 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. If the array object has register storage class, the behavior is undefined."

源:ISO / IEC 9899:2018(C18),6.3.2.1 / 4

Source: ISO/IEC 9899:2018 (C18), 6.3.2.1/4

该数组将转换为指针类型的表达式,而不是 lvalue

The array is converted to an expression of pointer type, it is not an lvalue.

编译器仅将 a 评估为& a [0] (指向 a [0] )。

The compiler just evaluates a to &a[0] (pointer to a[0]).


我知道数组名称会转换为指针。

"I understand that array names are converted to pointers."

数组并不总是转换为指向其第一个元素的指针。请看上面报价的第一部分。 F.e.当用作& a 时, a 不会衰减到指向其第一个元素的指针。而是获得指向整个数组 int(*)[3] 的指针。

An array does not always convert to a pointer to its first element. Look at the first part of the quote above. F.e. when used as &a, a does not decay to a pointer to its first element. Rather it gains a pointer to the whole array int (*)[3].

这篇关于指向指针衰减的数组是否已更改为指针对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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