Re: Another spinoza challenge



Phil Carmody said:

Kenneth Brody <kenbrody@xxxxxxxxxxx> writes:
<snip>

It appears that some people can't grasp the fact that passing a
variable by reference, and passing the address of a variable by
value
are not the same thing. (Yes, you can achieve the effect of
changing the caller's variable's value by doing pointer-by-value,
but that doesn't make them the same thing.)

It appears that some people can't grasp the fact that one can
effect pass by reference by passing variables by address.

Only by torturing the concept of pass by reference beyond its yield
point. If passing the address of a variable were equivalent to
passing by reference, then the following program would print 42:

#include <stdio.h>

void byref(int *p)
{
p = 42; /* bug - p was *not* passed by reference. */
}

int main(void)
{
int i = 6;
byref(&i);
printf("%d\n", i);
return 0;
}

Instead, the above provokes a diagnostic message (and rightly so).

In such hallowed halls as these I hesitate to mention Another Language
but, in Another Language, the distinction between
pass-pointer-by-value and pass-object-by-reference is much clearer:

void byref(int &r)
{
r = 42; /* this reference does not require dereferencing */
}
void byval(int *p)
{
*p = 42; /* this pointer must be dereferenced to be useful */
}
int main()
{
int x = 6;
byref(x); /* look ma, no 'ands */
int y = 6;
byval(&y); /* but here, the ampersand is required */
return 0;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
This line unintentionally left unblank
.



Relevant Pages

  • Re: Whats the difference of those two ways of passing parameters
    ... Parameter passing in C is beautifully simple: ... reference", you just pass a pointer and modify the object through that. ... In languages like Java, ... int foo = 42; ...
    (comp.lang.c)
  • Re: pointer syntax
    ... > really passing a reference (which is like an old-school pointer, ... > a copy of the primitive's value and passing that. ... build them yourself with pointers. ... mean a var parameter, write a var parameter. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Passing by reference
    ... It depends upon the reason one is passing the reference. ... instantiate all relationships via a constructor so there will always be ... I naturally think of 'getter' methods as 'knowledge ...
    (comp.object)
  • RE: Passing arguements by reference
    ... even without using the ref keyword. ... Passing a value type by value ... Passing a value type by reference ... When you pass value types they by default get copied, so passing an int into ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Using ref
    ... the address of a variable that contains a reference to an object, ... the C version is passing the address of a variable that contains the   ... Inasmuch as the specification is unambiguous, there is a single truth. ... Every time he states the incorrect view, I will feel compelled to correct his incorrect statements. ...
    (microsoft.public.dotnet.languages.csharp)