3 ms·
It is not that simple, because in C you may chain a great number of prefix and postfix operators. It is frequent to have multiple "*", "[]", "." and "->". Now
by adrian_b 3y ago
It is not that simple, because in C you may chain a great number of prefix and postfix operators.
It is frequent to have multiple "*", "[]", "." and "->".
Now you have the simple rule of executing first the postfix operators from left to right, then the prefix operators from right to left.
If some special rules had to be invented to avoid writing the parentheses in "(*x).y", after adding several "*", "[]" and "." it would become impossible to understand the right evaluation order.
When all of "*", "[]" and "." are postfix, they are just executed in order from left to right, which is easy to understand.
While sometimes auto-dereferencing a pointer would be convenient, in C you want frequently to get the value of a link, or to do pointer arithmetic. With auto-dereferencing, some new ways of using an "address of" operator would have to be invented, which are unlikely to be more simple than the current rules.
Like I have said, the 2 languages that have introduced the concept of pointers were Euler, with explicit dereferencing, and CPL, with implicit auto-dereferencing.
Both are valid ways to design a programming language, but in both case a lot of rules must be added to cover all the use cases, which would have to be more complex in the languages with auto-dereferencing, so these languages usually solve this problem lazily, by just prohibiting some uses, e.g. by prohibiting pointer arithmetic.
- renox 3y agoThe problem with postfix * for dereferencing is that it would look like a multiplication.. That's why I would have used a postfix @ for dereferencing.
- tsimionescu 3y agoI believe GP means that "a.b" should be enough for pointers as well, you don't need "a->b" or "(*a).b". It's not like pointers can have fields in C, so the meaning would have been unambiguous.
- adrian_b 3y agoSorry, I had posted a partial reply before finishing writing the complete one. As I have written above, a language with implicit dereferencing needs additional syntactic means for denoting the cases when the values of the pointers are needed, e.g. for doing pointer arithmetic, which is frequent in C. If dereferencing would have been made implicit, that would have required a large number of changes in the language. C++ has introduced pointers with implicit dereferencing, i.e. what C++ calls references, but due to the fact that the other syntax changes that are needed have not been made, because they would break compatibility, the C++ references can replace C pointers only in a subset of their uses.
- dataflow 3y ago> If dereferencing would have been made implicit You're misunderstanding the comment. They aren't asking for implicit derefs. They're asking for explicit derefs, using * or . instead of * or ->
- psd1 3y agoYour and GP's make no sense unless the reader knows that * is a markdown directive and infers where you typed them. Which is ironic when discussing overloading!
- dataflow 3y agoIn confused, where in my comment does the Markdown syntax come into play? You're not seeing italics or backslashes are you?
- psd1 3y agoI see now - Hacki renders italics, but the browser doesn't. Excuse my confusion, this possibility didn't occur to me.
- fanf2 3y agoHacker News does not use Markdown.
- masklinn 3y agoWhile that’s technically true, it does use a severely cut down version thereof which is worse in every way: it only supports paragraphs, code blocks, and emphasis so the ability to format is extremely limited but the emphasis can still screw you over, which requires an explicit preview (or posting) to notice. And I think escaping emphasis was only introduced somewhat recently? I do remember that for the longest time you basically had to trick HN into not breaking your comments by using a different character in stead.
- 3y ago
- flohofwoe 3y agoI really prefer that '.' and '->' remain separate, because it makes it immediately clear when a potentially expensive pointer derefence is happening: x = a->b->c->d->e; ...is a pointer-hunting-nightmare / potential cache-miss-galore. x = a.b.c.d.e; ...no problem, the whole chain is resolved at compile time into a single offset and results in at most one memory access. x = a.b.c->d.e; ...makes it immediately clear that c->d is a pointer access and everything else isn't. PS: ...and of course C++ messed this simple rule up with the introduction of references.
- nuancebydefault 3y agoWhen stepping up from C to C++, it feels ultimately unlogical to use an ampersand to indicate a reference in a declaration, while in an assigment or actual parameter it means address of. Where they out of special chars?