我不时遇到“衰减”一词,例如当作为函数参数传入的数组衰减为指针时,或者当函数衰减为函数指针时。如果我正在编写 ac 编译器,我在哪里可以找到正式定义的“衰减”一词,以及它发生的所有情况都将在哪里记录?
问问题
307 次
4 回答
3
标准中的官方术语是“左值转换”。在当前版本的标准 (C11) 中,您可以在 6.3.2.1 p3 中找到它。
于 2012-05-02T16:14:40.040 回答
1
第二次谷歌搜索结果如下:
The K&R method of reducing arrays to pointers
---------------------------------------------
K&R tried to create a unified treatment of arrays and pointers, one that
would expose rather than hide the array equation in the compiler's code.
They found an elegant solution, albeit a bit complicated. The "ugly"
array equation is replaced in their formulation by four rules:
1) An array of dimension N is a 1D array with
elements that are arrays of dimension N-1.
2) Pointer addition is defined by:
ptr # n = ptr + n * size(type-pointed-into)
"#" denotes here pointer addition to avoid
confusion with ordinary addition.
The function "size()" returns object's sizes.
3) The famous "decay convention": an array is
treated as a pointer that points to the
first element of the array.
The decay convention shouldn't be applied
more than once to the same object.
4) Taking a subscript with value i is equivalent
to the operation: "pointer-add i and then
type-dereference the sum", i.e.
xxx[i] = *(xxx # i)
When rule #4 + rule #3 are applied recursively
(this is the case of a multi-dimensional array),
only the data type is dereferenced and not the
pointer's value, except on the last step.
于 2012-05-02T15:35:02.383 回答
0
数组名称衰减意味着它的值被视为(=具有类型和值)作为指向其第一个元素的指针。请注意,数组名称并不总是衰减(例如,不作为 sizeof 运算符的操作数)。不完美的同义词可能被视为或重写为或简单地用作。
于 2012-05-02T16:00:59.237 回答
0
C 标准是回答此类问题的唯一方法。
如果你想购买官方文件,那么你需要寻找ISO/IEC 9899:2011。或者可能更喜欢 1999 年或 1989 年的旧标准。
或者,2011 标准的最终草案可在此处获得,并且应该足够接近: http ://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
于 2012-05-02T15:44:24.523 回答