passing by refference

Doug Quale quale1 at charter.net
Thu May 15 21:40:53 EDT 2003


Erik Max Francis <max at alcyone.com> writes:

> Doug Quale wrote:
> 
> > void main() {
> 
> main returns int.

I copied Tim's example making the minimum changes necessary to
demonstrate the point.  (His example had void main() also.)

Flame away if it makes you happy.

> > 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.)

That's exactly the point.  The r-value of a C array is a pointer.  (In
your words, "an array name decays to a pointer to the first element.)
The r-value gets passed; C is call-by-value.  C array arguments show
the same behavior as arguments passed in Python.

> > 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.

Your point is?  I said some people might say structures are passed by
value.  Those people are right, since all C arguments are passed by
value.




More information about the Python-list mailing list