News for hackers. Stuff that matters.
Re:Are pointers and arrays equivalent in C? (2009) by lfowles on Saturday March 21, 2015 @02:44PM (#9243767)
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 :\
Re:Are pointers and arrays equivalent in C? (2009) by pikachu_is_cool on Saturday March 21, 2015 @02:56PM (#9243797)
C doesn't have pass by reference.
Re:Are pointers and arrays equivalent in C? (2009) by ArkyBeagle on Saturday March 21, 2015 @03:41PM (#9243949)
It has "pass a pointer by value", which works out conceptually to the same thing, only with slightly different syntax.
Re:Are pointers and arrays equivalent in C? (2009) by pikachu_is_cool on Monday March 23, 2015 @08:59PM (#9254317)
Yeah I was talking about this:

> was using & to show pass-by-reference

Re:Are pointers and arrays equivalent in C? (2009) by krylon on Saturday March 21, 2015 @03:28PM (#9243897)
No, 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.

Re:Are pointers and arrays equivalent in C? (2009) by Buge on Saturday March 21, 2015 @03:30PM (#9243907)
C++ 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.

Re:Are pointers and arrays equivalent in C? (2009) by eliben on Saturday March 21, 2015 @06:42PM (#9244506)
This 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.
Re:Are pointers and arrays equivalent in C? (2009) by userbinator on Saturday March 21, 2015 @08:47PM (#9244984)
I'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.

Re:Are pointers and arrays equivalent in C? (2009) by davvid on Sunday March 22, 2015 @01:59AM (#9245781)
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

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.

Re:Are pointers and arrays equivalent in C? (2009) by Buge on Monday March 23, 2015 @01:46AM (#9249640)
Without 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.

Re:Are pointers and arrays equivalent in C? (2009) by userbinator on Monday March 23, 2015 @11:10AM (#9251320)
Given that references are implemented with pointers, those special functions are really taking pointer parameters anyway. Syntax-wise things might look a little different, but I don't think it poses much of a real problem.
Re:Are pointers and arrays equivalent in C? (2009) by Buge on Monday March 23, 2015 @03:48PM (#9252966)
But you lose the constant syntax that allows STL containers to work. vector<int> and vector<Person> work because vector can be templated, and

    int i1;
    int i2 = i1;

    Person p1;
    Person p2 = p1;
both have the same syntax. If suddenly p2 = p1 no longer compiles and you have to do p2 = &p1 that looks really weird because the two sides of the = have different types and vector<Person> no longer will compile. On the other hand if we say all overloaded operators automatically convert operands to their addresses, then we are sort of back where we started with references: you can modify a variable inside a function without having to say &. Only this would be much less flexible than what we have now because sometimes you want an overloaded operator to take something by value, but wouldn't be able to do it.
Re:Are pointers and arrays equivalent in C? (2009) by JoeAltmaier on Monday March 23, 2015 @03:53PM (#9252996)
I'm the same way! Should have used '@' to create a pointer, and '*' to deref one. They got it all twisted up in 'C'.
Re:Are pointers and arrays equivalent in C? (2009) by ArkyBeagle on Saturday March 21, 2015 @03:39PM (#9243939)
I think the easiest compare-contrast is with Pascal, and 'C' just shows the implementation details of pass-by-reference a bit more than Pascal.