5 ms·
The problems caused by "*" in C/C++ declarations and also in expressions are caused mostly by the mistake that "*" is a prefix operator instead of being a postf
by adrian_b 5y ago
The problems caused by "*" in C/C++ declarations and also in expressions are caused mostly by the mistake that "*" is a prefix operator instead of being a postfix operator.
This mistake has already been recognized by Dennis Ritchie a very long time ago, but it could not be corrected as it would break backward compatibility.
In 1965, the language EULER by Niklaus Wirth & H. Weber was the first high-level language with explicit pointer variables (previous languages, e.g. LISP I, FORTRAN IV and CPL used pointers only implicitly) and with the 2 operators "address-of" and indirect addressing, the ancestors of C "&" and "*".
However in EULER the indirection operator was postfix, as it should be. Niklaus Wirth kept this operator as postfix in his later languages, e.g. in Pascal.
With a postfix indirection operator, when you write some element of a hierarchical organization of data, which can include an arbitrary number of the 3 operations, array indexing, structure/class member selection and pointer indirection, all the operations are written and executed from the left to the right, without any parentheses and without any ambiguities. (also the C operator taken from PL/I, "->", is no longer needed, as it has no advantage over writing "*.").
This simple change would have avoided most pitfalls of the C declarations.
- DeathArrow 5y agoI don't think prefix usage of * operator is a big flaw. I've learned C/C++ in high school and never had a an issue with it. I've understood that the operator is applied to the variable, not to the type and that declaring a pointer is not the same thing as dereferencing it . Even if someone may have made a mistake when declaring a pointer in C or C++ it is something they can get over pretty fast.
- habibur 5y agoI didn't understand it either. Than learned assembly for a few months for a project -- switched back to C and everything came as natural.
- ncmncm 5y agoDereference syntax in C++ is very, very fixable. C++ could easily adopt a postfix operator^ as a synonym for prefix op*, even at this late date. Then (*p)->m could be written p^->m, or p^^.m. (The last is clearly best.) Rust, too, could do the same, for the same benefit. And C could, too, if there were any point. The only people to complain would be IDE vendors. If you were then allowed to declare a pointer argument like "char p^", or even "char^ p", you could begin to dispense with use of "*" as a unary operator. Declaring "char^ p, q" would mean both p and q are pointers, eliminating another source of beginners' confusion.
- jeltz 5y agoRust copied this misstake. Rust would have been neater if taking references and dereferencing were postix operators. At least they made await a postfix operator.
- ModernMech 5y agoTo Rust's credit, 90% of the time I find I don't need to dereference much at all. For example, when you make a method call on a reference, you don't have to dereference first, and there is no -> operator in Rust. The only time I ever really have to dereference manually in my experience is if I'm matching on some struct reference, and I want to take some field and put it in a new place. e.g. match &foo { Foo{bar} => Baz{quux: *bar}, _ => (), } (not valid Rust but you get the point)
- Koshkin 5y agoNote that in C at least the '->' is completely redundant and the '.' alone would suffice. (C++ is a somewhat different story.)
- scoopdewoop 5y agoNot without parentheses, which is why -> was introduced.
- ModernMech 5y agoOnly because of arbitrary precedence rules though. They didn't have to make . higher than * *foo.bar() could be read as "dereference foo and then evaluate bar() on the result".