2011年2月11日 星期五

傳指標呼叫屬於傳值呼叫的一種

光看到這個標題,許多人可能會覺得不以為然。不過,如果詳讀過C語言發明人之著作The C programming Language, Brian W. Kernighan and Dennis M. Ritchie., Prentice-Hall, 1988.就可以發現下列文字

One aspect of C functions may be unfamiliar to programmers who are used to some other languages, particularly Fortran. In C, all function arguments are passed "by value''. This means that the called function is given the values of its arguments in temporary variables rather than the originals. This leads to some different properties than are seen with "call by reference"' languages like Fortran or with var parameters in Pascal, in which the called routine has access to the original argument, not a local copy.


至於我們要如何證明"pass by pointer"也是"call by value"這個事實呢?詳見C語言初學指引第四版 7-29頁。