I don't think there's anything crazy about what you've said. Using .
for pointers to structs would work.
However, I like the fact that pointers to structs and structs are treated differently.
It gives some context about operations and clues as to what might be expensive.
Consider this snippet, imagine that it's in the middle of a reasonably large function.
s.c = 99;
f(s);
assert(s.c == 99);
Currently I can tell that s
is a struct. I know that it's going to be copied in its entirety for the call to f
. I also know that that assert can't fire.
If using .
with pointers to struct were allowed, I wouldn't know any of that and the assert might fire, f
might set s.c
(err s->c
) to something else.
The other downside is that it would reduce compatibility with C++. C++ allows ->
to be overloaded by classes so that classes can be 'like' pointers. It's important that .
and ->
behave differently. "New" C code that used .
with pointers to structs would no probably not be acceptable as C++ code any more.
Well there clearly isn't any ambiguity or the proposal couldn't be made. The only issue is that if you see:
p->x = 3;
you know p
is a pointer but if you allow:
p.x = 3;
in that circumstance then you don't actually know, which could potentially create problems, particularly if you later cast that pointer and use the wrong number of levels of indirection.
A distinguishing feature of the C programming language (as opposed to its relative C++) is that the cost model is very explicit. The dot is distinguished from the arrow because the arrow requires an additional memory reference, and C is very careful to make the number of memory references evident from the source code.
Well, if you really wanted to introduce that kind of functionality into the specification of C language, then in order to make it "blend" with the rest of the language the logical thing to do would be to extend the concept of "decay to pointer" to struct types. You yourself made an example with a function and a function pointer. The reason it works that way is because function type in C decays to pointer type in all contexts, except for sizeof
and unary &
operators. (The same thing happens to arrays, BTW.)
So, in order to implement something similar to what you suggest, we could introduce the concept of "struct-to-pointer decay", which would work in exactly the same way as all other "decays" in C (namely, array-to-pointer decay and function-to-pointer decay) work: when a struct object of type T
is used in an expression, its type immediately decays to type T*
- pointer to the beginning of the struct object - except when it's an operand of sizeof
or unary &
. Once such a decay rule is introduced for structs, you could use ->
operator to access struct elements regardless of whether you have a pointer to struct or the struct itself on the left-hand side. Operator .
would become completely unnecessary in this case (unless I'm missing something), you'd always use ->
and only ->
.
The above, once again, what this feature would look like, in my opinion, if it was implemented in the spirit of C language.
But I'd say (agreeing with what Charles said) that the loss of visual distinction between the code that works with pointers to structs and the code that works with structs themselves is not exactly desirable.
P.S. An obvious negative consequence of such a decay rule for structs would be that besides the current army of newbies selflessly believing that "arrays are just constant pointers", we'd have an army of newbies selflessly believing that "struct objects are just constant pointers". And Chris Torek's array FAQ would have to be about 1.5-2x larger to cover structs as well :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With