Re: why use -> (not .) with pointers?



>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.
.



Relevant Pages

  • Re: == operator on struct
    ... > even if they are equal and they are not part of a struct) - what has ... Another issue is struct members that happen to be unions. ... way to determine which member of the union should be compared. ... > I thought that the compiler may not touch the holes. ...
    (comp.lang.c)
  • Re: komplexes Problem mit Funktionszeigern
    ... Die Funktion gibt einen struct "by value" zurueck und bei der ... am Borland Compiler, da der Microsoft Compiler ... Man kann Argumente fuer eine Funktion z.B auf dem Stack uebergeben. ... Das funktioniert natuerlich nur fuer "Basis Typen" die in ein Register passen. ...
    (de.comp.lang.c)
  • Re: Copying from one struct to another, simple assignment?
    ... cases where struct comparison is useful. ... For some languages (such as Ada), ... it's easy (for the compiler) to handle the easy cases. ... finds a member that's unequal. ...
    (comp.lang.c)
  • Re: Naming typedefs
    ... problem that a scalar could be returned in the A register ... to have waste precious cpu cycles copying the struct there ... of the hardware or twentieth century compiler technology. ... # to include the header defining it into every other header (assuming ...
    (comp.unix.programmer)
  • Re: assembly language and reverse engineering
    ... "return arguments to struct pointed to by register" and ... assumptions of CDECL-like conventions (also supports STDCALL, ... arg for handling struct return (slightly compiler, and compiler version, ...
    (alt.lang.asm)