5 ms·
Much of my initial confusion with C style pointers was using & to show pass-by-reference and to get the address of a variable. It still doesn't make sense, but
by lfowles 12y ago
Much of my initial confusion with C style pointers was using & to show pass-by-reference and to get the address of a variable. It still doesn't make sense, but at least I've got it deeply ingrained now :\
- pikachu_is_cool 12y agoC doesn't have pass by reference.
- krylon 12y agoNo, it does not. That is why pointers are so important in C. But to a newbie, the difference between a local variable and a pointer to it can be rather confusing, and the fact that with arrays this difference becomes rather blurry unless one has a good mental model of what the compiler turns your code into, makes it even more confusing. Given that some data structure such as linked lists are highly inconvenient or even impossible to implement without dynamic memory management, one is pretty much bound to writing code that is either primitive (not always a bad thing) or horrible to look at (and maintain) until one understands pointers.
- ArkyBeagle 12y agoIt has "pass a pointer by value", which works out conceptually to the same thing, only with slightly different syntax.
- pikachu_is_cool 12y agoYeah I was talking about this: > was using & to show pass-by-reference
- Buge 12y agoC++ hates creating new words so they try to reuse old stuff as much as possible with new meanings. There are 3 meanings of static, 2 meanings of auto, 2 meanings of delete, 2 meanings of default.
- eliben 12y agoThis is not limited to C++. All widely-used, "old" programming languages shun away from adding keywords, because it can break previously working code. Python is the same way too.
- userbinator 12y agoI'd say that in the case of pass-by-reference, the symbol they reused makes sense: references are basically syntactic sugar for a limited form of "automatically-dereferencing pointers", meaning that creating a reference involves taking an address, and so & would be the logical choice. Personally I don't think it's so useful of a feature; it saves typing a few asterisks but obscures the fact that you're actually modifying some other variable.
- davvid 12y agoPersonally I don't think it's so useful of a feature; it saves typing a few asterisks but obscures the fact that you're actually modifying some other variable There are many real-world use cases where references are exactly the right feature, and the const-ness of that reference should make the intentions clear. Performance-critical code is one place where not having the ability to pass large objects around by reference would be a performance killer. References allow us to avoid making unnecessary copies. Yes, you can accomplish the same thing with a pointer, but raw pointers are much less common in modern C++. It's not just syntactic sugar; a reference conveys information. When writing a function you can eschew null pointer checks when using references, whereas a function taking a (raw) pointer should handle the null pointer case since technically you can call foo(nullptr) and it'll compile. With a reference bar must exist for the foo(bar) expression to compile when calling void foo(const Bar& bar), so it's safer, simpler code.
- Buge 12y agoWithout references there would be tons of problems. How would you implement a copy constructor or copy assignment operator without them? Those two things are necessary for user defined types to be able to be treated like built in types, which is necessary if you want templates to be fully generic, which is essential for all the STL containers.
- ArkyBeagle 12y agoI think the easiest compare-contrast is with Pascal, and 'C' just shows the implementation details of pass-by-reference a bit more than Pascal.
- JoeAltmaier 12y agoI'm the same way! Should have used '@' to create a pointer, and '*' to deref one. They got it all twisted up in 'C'.