pointers and dot operator

S

Serve Laurijssen

What are the disadvantages if C would allow referencing pointer to struct
with the dot operator?
Such as

struct X *x = ...;

x.y = 10;

Im not crying to get this added to the standard as Im happy using the arrow
operator anyway. I'm just interested in the disadvantages. Good things about
this style:

* it makes it easier for new programmers to learn the language as pointers
tend to confuse in the beginning. They wont be scared off easily and they
can learn the language at a lower pace
* Universal notation for struct members
* wont break existing code when added to a compiler

The only disadvantage I can see is that now its not immediately clear
anymore when you see x.y that x is a pointer but this is a minor
disadvantage IMO. You almost never need to know if its a pointer or not,
only in the case of bugs when x is a NULL pointer but then you're analyzing
the code thoroughly anyway, plus the tools of today can show you very well
if its a pointer or not.

Any bigger disadvantages?
 
P

Paul N

You mean, apart from pointlessly confusing object and
pointer-to-object, thus making pointers still more puzzling to
newbies than they already are?

Sometimes providing an alternative, non-logical way of doing things is
considered an advantage. For instance, when assigning the address of
an actual function to a function pointer you are allowed to miss off
the "&". (Or, as some in the group would put it, you are allowed to
add an "&".)
 
S

Serve Laurijssen

Richard Heathfield said:
You mean, apart from pointlessly confusing object and
pointer-to-object, thus making pointers still more puzzling to
newbies than they already are?


I dont think it would make them more puzzling as I said before because they
can postpone thinking about pointer to members later when they're more
comfortable with pointers. In the meantime they can just use dot operator.
But we will never know until it is used as an experiment.
 
D

David Tiktin

I dont think it would make them more puzzling as I said before
because they can postpone thinking about pointer to members later
when they're more comfortable with pointers. In the meantime they
can just use dot operator. But we will never know until it is used
as an experiment.

But won't it lead to puzzlement of a different kind? Suppose a and b
are of the same type. Given:

/*
Some code which obscures whether a and b are
structs or pointers
*/

a.x = 0;

b.x = 1;

/* Some code using a and b but not changing b.x */

a = b;

b.x = 2;

What is the value of "a.x"? If a and b are pointers, a.x (a->x) == 2
since a == b. If a and b are structs, a.x (a.x) == 1 since "a = b"
didn't make them the same object. You're still going to have to
explain that difference, and now it looks confusing, even to me! I
think Richard is right about this one.

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top