Re: why use -> (not .) with pointers?
- From: Chris Torek <nospam@xxxxxxxxx>
- Date: 30 Jun 2005 18:00:53 GMT
>Felix Kater <f.kater2@xxxxxxx> wrote:
>> when accessing the variables in a struct: What's the reason why in C
>> you have -> and . instead of only . ? Are there cases in which the
>> compiler couldn't figure out what to do?
In article <frcdp2-o3l.ln1@xxxxxxxxxxxxxxxx>
<lawrence.jones@xxxxxxx> gives the correct answer:
>Not anymore. But back in the dark ages, C was much less concerned about
>types than it is today and would allow pretty much *anything* as the
>left operand of . or ->. In that environment, the compiler couldn't
>figure out what to do, so having two different operators was essential.
but it might serve to be a bit more detailed.
The "true need" for separate "." and "->" operators went away
sometime around 1978 or 1979, around the time the original K&R
white book came out.
Before then, Dennis' early compilers accepted things like this:
struct { char a, b; };
int x 12345; /* yes, no "=" sign */
main() {
printf("%d is made up of the bytes %d and %d\n", x,
(x.a) & 0377, (x.b) & 0377);
}
(in fact, in an even-earlier version of the compiler, the syntax
was "struct (" rather than "struct {". The syntax above is what
appeared in V6 Unix. I have read V6 code, but never used the V6
C compiler myself.)
Note that we have taken the "a" and "b" elements of a plain
"int", not the "struct" that contains them. The "." operator
works on *any* lvalue, in this early C, and all the structure
member names must be unique -- no other struct can have members
named "a" and "b".
We can (and people did) also write things like:
struct rkreg { unsigned rkcsr, rkdar, rkwc; };
...
/* read disk sector(s) */
0777440->rkdar = addr;
0777440->rkwc = -(bytecount / 2);
0777440->rkcsr = RK_READ | RK_GO;
Note that the "->" operator works on *any* value, not just pointers.
Since this "early C" did not look at the left hand side of the
"." and "->" operators, it really did require different operators
to achieve different effects. These odd aspects of C were fixed
even before the very first C book came out, but -- as with the
"wrong" precedence for the bitwise "&" and "|" operators -- the
historical baggage went along for the ride.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
.
- Follow-Ups:
- Re: why use -> (not .) with pointers?
- From: P.J. Plauger
- Re: why use -> (not .) with pointers?
- References:
- why use -> (not .) with pointers?
- From: Felix Kater
- Re: why use -> (not .) with pointers?
- From: lawrence . jones
- why use -> (not .) with pointers?
- Prev by Date: Re: why use -> (not .) with pointers?
- Next by Date: Re: why use -> (not .) with pointers?
- Previous by thread: Re: why use -> (not .) with pointers?
- Next by thread: Re: why use -> (not .) with pointers?
- Index(es):
Relevant Pages
|