減少陣列的指針的K個& R法:(從arrays and pointers in C的摘錄)減少陣列的指針的K&R方法
ķ&ř試圖建立數組和指針的統一處理,一個 會公開而不是隱藏編譯器代碼中的數組公式。 他們發現了一個優雅的解決方案,儘管有點複雜。 「醜陋」 數組公式是由四個規則取代了他們的配方:
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.
我不明白這是什麼意思
- 衰減公約不應當適用更比一次到同一個對象(規則#3)。
- 遞歸應用規則#4 +規則#3(這是多維數組的情況),只有數據類型被取消引用,而不是指針的值,除了最後一步。
有人可以用例子解釋嗎?
@EricPostpischil;源已在問題被引用(在第一行的末尾) – haccks