c中的数组衰减是什么以及何时发生? [英] what is array decay in c and when it happen?

查看:64
本文介绍了c中的数组衰减是什么以及何时发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习C语言.我想知道阵列衰减"的含义是什么,何时发生.

I am currently studying C language. I wonder what 'array decaying' means, and when it happens.

我想知道下面的两个变量是否以相同的方式解释.

And I wonder if the two variables below are interpreted in the same way.


char(*zippo)[2] = NULL;
char zippo2[4][2];

zippo = (char(*)[2])malloc(sizeof(char[2]) * 4);


推荐答案

来自C标准(6.3.2.1左值,数组和函数指示符)

From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)

3除非它是sizeof运算符的操作数或一元&运算符,或者是用于初始化数组的字符串文字,类型为类型数组"的表达式将转换为类型为要输入的指针"的表达式指向初始数组对象的元素,不是左值.如果数组对象具有寄存器存储类,其行为是不确定的.

3 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.

下面的两个变量

char(*zippo)[2] = NULL;
char zippo2[4][2];

具有不同的类型.第一个是指向 char [2] 类型的对象的指针.第二个是一个二维数组,其中包含四个类型为 char [2] 的元素.

have different types. The first one is a pointer to an object of the type char[2]. The second one is a two-dimensional array with four elements of the type char[2].

当在表达式中使用数组 zippo2 时,除了引号中列出的表达式之外(例如,将其与sizeof运算符配合使用),则其指示符将隐式转换为指向其第一个元素的指针,并具有与变量 zippo 的类型相同.

When the array zippo2 is used in expression except the expressions listed in the quote (as for example using it with the sizeof operator) then its designator is implicitly converted to pointer to its first element and has the same type as the variable zippo.

这篇关于c中的数组衰减是什么以及何时发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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