My previous comments about call by {value,reference,name} were confused and confusing. Please ignore them and let me start over.
IMO, a beautiful thing about C is how simple and deterministic it is. Once you learn the simple rules of C, it should be easy to determine, by simple LOCAL inspection, what any small piece of C code does — this is NOT the case with C++ which "hides" relevancies in class definitions. (Pointers, and their distinction from arrays, may be hard to grasp at first. But understanding them is worth the time investment.)
Are you saying it's equivalent to C's call by name, but just uses different syntax?
Yes. Swammerdami, can you point me to any documentation of the difference in plain C between:
- Call by value
- Call by name
- Call by reference
C has exactly ONE call mechanism:
Call by value.
In the C function
Code:
whatever foo (..., some_type george, ...) {
... george ...
... george ...
... george ...
}
you can change EVERY occurrence of "
george " to "
*george " (or "
(*george) " as needed) and get, in effect,
call by reference. But this is NOT a special feature of C; it is not a "cutesy" way to get "call by reference" without introducing a new key-word. It is just a natural and inevitable consequence of the way C's simple unary " * " operator works.
I don't NEED to think about "call by reference" in C. I just need to know C's " * " operator and the useful idea follows AUTOMATICALLY.
How about the weird overloading of C++'s " & " operator? Is there a simple way to transform it into correct C syntax? If so, why bother to have it? If not, what makes it useful? I'll guess the answer to the second question, if any, has something to do with hobbling the programmer's ability to write through pointers. I am happy to code UNHOBBLED, but thanks anyway!
[off-topic] There is one case where C, despite good intentions, "gets it wrong," sort of:
Code:
int *xp = 0;
int* zp = 0;
int *yp;
yp = 0;
xp and yp are handled identically in the above fragment but they APPEAR different (*xp=0 vs yp=0). Some avoid confusion by changing the position of white-space as I've shown with zp. My work-around is just to avoid initializers of that form. They have no purpose except to reduce the line-count slightly.