6 ms·
> "pass by reference" is an incorrect phrasing No, it isn't. In C, a pointer is a reference. It supports arithmetic, but it works like a reference as well, so
by cbd1984 12y ago
> "pass by reference" is an incorrect phrasing
No, it isn't.
In C, a pointer is a reference. It supports arithmetic, but it works like a reference as well, so the name applies.
What might confuse people is that in C, you do have to specifically ask for a reference to something, whereas in Java, everything which isn't a primitive type is already a reference.
It's true your code can't dereference those references such that it gets access to all of the data in a List object, for example. C works the same way, again: It's entirely possible to say something like 'typedef struct foo mfoo;' and then declare all of your functions to take pointers to type mfoo, and client code will never be able to get its hands on a struct foo object. FILE pointers work just like this, for example.
- rifung 12y agoI don't think a pointer in C is really a reference though? If you pass a pointer to a function, then you are saying, hey allocate a new variable to put on the stack which is a pointer. If you pass a reference, then at the language level, you are saying hey I want that function to use THIS variable that I already have. While the implementation may certainly be free to use a pointer, they are not fundamentally the same concepts. Or so I think.
- DonHopkins 12y agoWhat something IS is independent of how you PASS it. An integer isn't a reference, but you can pass a reference to an integer in C++ (to implement swap, for example), while you can't do that in Java.
- nknighthb 12y agoA pointer in C is a reference (see ISO 9899:1999 § 6.2.5) under siege by nasal demons (see § 6.3.2.3). The implementation isn't really required to do many of the things people assume it is. (It's ironic, by the way, that in seeking to be more pedantic, you reference stacks. Stacks are not part of C.)
- jjnoakes 12y agoA C pointer is not a reference in the way we are using the word reference in this discussion (as in pass-by-reference, as in an alias for another value without applying any operators). Sure, the C standard uses the English word "reference", as in "refers to" and "referred to", but letting that confuse you without understanding the meaning of the word in context is what lead to this Java mess in the first place. Don't bring C into it too.
- nknighthb 12y agoI'm not confused at all, thank you. I'm of the long-standing and well-informed opinion that "pass-by-reference" and "pass-by-value" are worthless terms that need to die, and that this entire conversation is an exercise in useless academic pedantry.
- jjnoakes 12y agoBased on your previous reply, you are indeed confused. Reference is a long-established term, and using it to refer to C pointers is typically considered incorrect. In addition, pass-by-reference and pass-by-value are very useful terms for anyone interested in accurately speaking about and understanding what will happen when they run the program they write. There are people who consider accuracy and understanding to be pedantry. More often than not, in my experience, they are a larger source of bugs than those who strive to truly understand how their tools work.
- nknighthb 12y ago> Reference is a long-established term, and using it to refer to C pointers is typically considered incorrect. I am not confused, I disagree. > In addition, pass-by-reference and pass-by-value are very useful terms for anyone interested in accurately speaking about and understanding what will happen when they run the program they write. The terms would be useful if people agreed on their meaning (they do not) and if languages actually followed one way or the other consistently (they do not). That is the very reason this thread exists. If you wish to accurately speak about what will happen, then speak about it. Describe what actually happens, instead of spending your time arguing about which round hole the square peg fits in better. > There are people who consider accuracy and understanding to be pedantry. In practice, pass-by-value and pass-by-reference provide neither accuracy nor understanding. Stop using them, and these arguments instantly vanish. > More often than not, in my experience, they are a larger source of bugs than those who strive to truly understand how their tools work. That I strive to truly understand how my tools work is the very reason I reject these terms as worthless.
- learc83 12y agoPass by reference has a specific meaning and specific consequences. In C to be able to simulate some aspects of pas-by-reference you need a double pointer.