我偶尔会遇到“衰变”一词,例如当函数参数传入的数组衰减到指针时,或者当函数衰减到函数指针时。如果我正在编写一个c编译器,我会在哪里找到正式定义的“衰变”这个术语,并且会记录它出现的所有情况?
答案 0 :(得分:3)
标准中的官方术语是“左值转换”。在当前版本的标准(C11)中,您可以在6.3.2.1 p3中找到它。
答案 1 :(得分: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.
答案 2 :(得分:0)
C标准是回答这些问题的唯一方法。
如果您想购买官方文档,则需要查找 ISO / IEC 9899:2011 。或者可能更喜欢1999年或1989年的旧标准。</ p>
或者,2011标准的最终草案可以在这里找到,并且应该足够接近: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
答案 3 :(得分:0)
数组名称衰减表示其值被处理(=具有类型和值)作为指向其第一个元素的指针。请注意,数组名称并不总是衰减(例如,不作为sizeof运算符的操作数)。不完整的同义词可能被视为或被重写为或只是被用作。