Re: Another spinoza challenge
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Tue, 25 Aug 2009 05:09:58 +0000
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
.
- Follow-Ups:
- Re: Another spinoza challenge
- From: spinoza1111
- Re: Another spinoza challenge
- From: bartc
- Re: Another spinoza challenge
- From: Phil Carmody
- Re: Another spinoza challenge
- References:
- Another spinoza challenge
- From: spinoza1111
- Re: Another spinoza challenge
- From: Richard Heathfield
- Re: Another spinoza challenge
- From: spinoza1111
- Re: Another spinoza challenge
- From: Richard Heathfield
- Re: Another spinoza challenge
- From: Kenneth Brody
- Re: Another spinoza challenge
- From: Phil Carmody
- Another spinoza challenge
- Prev by Date: Re: tree errors
- Next by Date: Re: Another spinoza challenge
- Previous by thread: Re: Another spinoza challenge
- Next by thread: Re: Another spinoza challenge
- Index(es):
Relevant Pages
|