passing by refference

Erik Max Francis max at alcyone.com
Thu May 15 17:30:05 EDT 2003


Doug Quale wrote:

> void main() {

main returns int.

> Why are arrays different?  You and I both just said that C was call by
> value.  Well, C is call by value.  The r-value of a C struct is the
> struct, but the r-value of a C array is a reference to the first
> element in the array.

More basic than that.  An array as an argument to a function call is
treated precisely as if it were written as a pointer.  So when you
declare a function taking an array, you're really declaring a function
taking a pointer.  (You can even test this with sizeof; it will return
the size of the _pointer_, not the declared array.)

So you're not passing a reference to the first element, you're passing a
pointer by value.  (And if what you were passing on the calling side was
an array, you're just taking advantage of the fact that an array name
decays to a pointer to the first element.)

> Some people might explain it by saying that in C, scalars and
> structures are passed by value and arrays are passed by reference.

Structures are passed by value; if you have a struct as an argument, a
copy is made.

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ Life is not a spectacle or a feast; it is a predicament.
\__/  George Santayana




More information about the Python-list mailing list