note 55469 deleted from language.references.arent by bjori
| From: | bjori@php.net | Date: | Mon, 27 Mar 2006 21:22:53 +0000 |
| Subject: | note 55469 deleted from language.references.arent by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-106781@lists.php.net to get a copy of this message | ||
Note Submitter: ssb45 at cornell dot edu
----
References are not like pointers, period. In PHP, references are namespace aliases. In C/C++,
pointers are memory addresses. While PHP sees references as having unique attributes, a C/C++
pointer is only a pointer because of its treatment. (That's why you can cast other data types
to pointers if you're really brave, really stupid, or really smart.)
What the manual says you can't do in PHP,
<?php
function foo(&$var)
{
$var =& $GLOBALS["baz"];
}
foo($bar);
?>,
you can do in C/C++ (though not how ansonyumo at email dot com said). Anysonyumo's second
example would set baz to bar's value (99), not to bar itself. Since baz is an int, not an *int
(a pointer to an int), it can only store a value. To really do what the PHP example seems to do,
you would need to use code like this:
-+-+-
int bar = 99;
void foo(int* a)
{
a = &bar;
}
int main()
{
int foobarbaz = 1;
int *baz = &foobarbaz;
foo(baz);
printf("%d\n", *baz); // sends "99" to stdout
*baz = 100;
printf("%d\n", bar); // sends "99" to stdout
return 0;
}
-+-+-