passing by refference

Daniel Fackrell newsgroups.NOSPAM at dfackrell.mailshell.com
Fri May 30 11:55:59 EDT 2003


Doug Quale wrote:
<snip>
> Although you correctly said that C structures are passed by value in
> an earlier post, that simply doesn't guarrantee the behavior that you
> and FOLDOC think it does.  Here is a contrived but simple example.
>
> #include <stdio.h>
>
> struct s {struct s *self; int i, j, k;};
>
> void display(const char* tag, struct s x) {
>   printf("%s %d %d %d\n", tag, x.i, x.j, x.k);
> }
>
> void f(struct s x) {
>     display("inside before", x);
>     x.self->i = x.self->j = x.self->k = -1;
>     display("inside after", x);
> }
>
> int main() {
>   struct s t = {&t, 1, 2, 3};
>   display("before", t);
>   f(t);
>   display("after", t);
> }
>
> Which prints
>
> before 1 2 3
> inside before 1 2 3
> inside after -1 -1 -1
> after -1 -1 -1
>
> So C structures don't have the much cherished "no effect" properties
> either.  On the other hand, I've never seen anyone claim that C
> structures are not passed by value.  (I suppose someone will pop up to
> dispute that now.)  By the FOLDOC definition, neither C nor Pascal is
> call-by-value.  (Pascal of course has var parameters, but even Pascal
> value parameters would not be call-by-value by FOLDOC's confused
> definition.)

Neat example, but it simply muddies the issue.  Yes, it is possible to
modify anything that isn't protected by the OS, as long as you pass a
pointer to it.  Also, the output that you show is not what I get (gcc 2.96
on Red Hat 7.1):

before 1 2 3
inside before 1 2 3
inside after 1 2 3
after -1 -1 -1

Which highlights the fact that you are changing the original, and not the
copy that was passed.

--
Daniel Fackrell (newsgroups.NOSPAM at dfackrell.mailshell.com)
When we attempt the impossible, we can experience true growth.






More information about the Python-list mailing list