本文介绍了数组衰减到指针的情况是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只知道一种情况 当数组传递给函数时,它们会衰减为指针.任何人都可以详细说明数组会衰减为指针的所有情况.

I know only one case when arrays passed to a function they decay into a pointer.Can anybody elaborate all the cases in which arrays decay to pointers.

推荐答案

C 2011 6.3.2.1 3:

C 2011 6.3.2.1 3:

换句话说,数组通常会衰减到指针.该标准列出了不这样做的情况.

In other words, arrays usually decay to pointers. The standard lists the cases when they do not.

当您将数组与下标(例如,a[3])一起使用时,可能会认为数组会充当数组.但是,实际上发生的是:

One might think that arrays act as arrays when you use them with subscripts, such as a[3]. However, what happens here is actually:

  • a转换为指针.
  • 下标运算符作用于指针和下标,以产生一个指定对象的左值. (特别是,a[3]的计算结果为*((a)+(3)).即,a转换为指针,在该指针上添加3,然后应用*****运算符.)
  • a is converted to a pointer.
  • The subscript operator acts on the pointer and the subscript to produce an lvalue designating the object. (In particular, a[3] is evaluated as *((a)+(3)). That is, a is converted to a pointer, 3 is added to the pointer, and the ***** operator is applied.)

注意:C 2011文本包含" _Alignof 运算符".在标准的C 2018版本中已对此进行了更正,我已从上面的引用中删除了它. _Alignof的操作数始终是类型;您实际上无法将其应用于对象.因此,将C 2011标准包含在6.3.2.1 3.中是一个错误.

Note: The C 2011 text includes "the _Alignof operator." This was corrected in the C 2018 version of the standard, and I have elided it from the quote above. The operand of _Alignof is always a type; you cannot actually apply it to an object. So it was a mistake for the C 2011 standard to include it in 6.3.2.1 3.

这篇关于数组衰减到指针的情况是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 12:26