Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #170597

Re: Why does the -> operator exist ?

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: Why does the -> operator exist ?
Date 2023-06-27 12:23 -0700
Organization None to speak of
Message-ID <87fs6cso8n.fsf@nosuchdomain.example.com> (permalink)
References <trfLpf+N71iCzEwJm@bongo-ra.co>

Show all headers | View raw


Spiros Bousbouras <spibou@gmail.com> writes:
> I mean would it create an ambiguity if you could write  s.field
> both when  s  is a structure or union and when  s  is a pointer
> to a structure or union ?

There are languages that use the `.` operator for selecting a member
from either a structure or a pointer to a structure.  Since `.name` with
a pointer prefix has no other meaning, no ambiguity is introduced.  (Ada
is an example.)

In early C (before K&R1), `prefix.member` treated the prefix as a
structure with a member named `member`, regardless of how `prefix` was
actually defined.  Member definitions were associated with struct
definitions, but were not tied to them.  Given:

    struct foo {
        int x;
        int y;
    };

the name `x` meant `an int member at an offset of 0`, and `.x` could be
applied to any lvalue.  (This is why member names for standard library
types have unique prefixes, like the tm_sec, tm_min, ... members of
struct tm.)

Thus `foo.x` where foo is a pointer would extract an int from the bytes
making up the representation of the pointer object, while `(*foo).x` or
equivalently `foo->x` would dereference foo and extract an int from the
object foo points to.

Some time between 1975 and 1978, the rules were changed so a member name
applies only to the struct (or union) type in which it's defined, but
the meaning of the `.` operator was never updated.  It *could* have
been, but it's not clear to me that it would have been worth the effort.
The `->` operator would have had to be left in place to avoid breaking
existing code, leaving us with two different ways to write the same
thing.  Also, I would argue that using `->` rather than `.` when the
prefix is a pointer makes for more explicit code, which is a good thing;
it's important to know when you're dealing with pointers.
    
-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why does the  ->  operator exist ? Spiros Bousbouras <spibou@gmail.com> - 2023-06-27 09:12 +0000
  Re: Why does the  ->  operator exist ? felix@palmen-it.de (Felix Palmen) - 2023-06-27 15:01 +0200
    Re: Why does the  ->  operator exist ? Spiros Bousbouras <spibou@gmail.com> - 2023-06-27 14:09 +0000
      Re: Why does the  ->  operator exist ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-06-27 15:56 +0100
  Re: Why does the -> operator exist ? Bart <bc@freeuk.com> - 2023-06-27 16:06 +0100
    Re: Why does the -> operator exist ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-06-27 18:01 +0100
      Re: Why does the -> operator exist ? BGB <cr88192@gmail.com> - 2023-06-27 12:46 -0500
    Re: Why does the -> operator exist ? fir <profesor.fir@gmail.com> - 2023-08-09 00:08 -0700
      Re: Why does the -> operator exist ? fir <profesor.fir@gmail.com> - 2023-08-09 00:11 -0700
        Re: Why does the -> operator exist ? fir <profesor.fir@gmail.com> - 2023-08-09 00:23 -0700
  Re: Why does the  ->  operator exist ? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2023-06-27 15:13 +0000
  Re: Why does the  ->  operator exist ? Kaz Kylheku <864-117-4973@kylheku.com> - 2023-06-27 15:30 +0000
    Re: Why does the -> operator exist ? Bart <bc@freeuk.com> - 2023-06-27 17:05 +0100
      Re: Why does the -> operator exist ? Kaz Kylheku <864-117-4973@kylheku.com> - 2023-06-27 17:11 +0000
  Re: Why does the -> operator exist ? BGB <cr88192@gmail.com> - 2023-06-27 10:54 -0500
  Re: Why does the  ->  operator exist ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-06-27 12:23 -0700
    Re: Why does the  ->  operator exist ? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2023-06-27 15:33 -0600
    Re: Why does the  ->  operator exist ? Spiros Bousbouras <spibou@gmail.com> - 2023-06-28 16:31 +0000
      Re: Why does the  ->  operator exist ? Kaz Kylheku <864-117-4973@kylheku.com> - 2023-06-29 03:14 +0000
    Re: Why does the  ->  operator exist ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-07-20 18:57 -0700
      Re: Why does the  ->  operator exist ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-07-20 19:49 -0700
        Re: Why does the  ->  operator exist ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-08 05:49 -0700
          Re: Why does the  ->  operator exist ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-08-08 16:33 -0700
            Re: Why does the  ->  operator exist ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-14 04:34 -0700
              I love it... (Was: Why does the  ->  operator exist ?) gazelle@shell.xmission.com (Kenny McCormack) - 2023-08-14 12:41 +0000
              Re: Why does the  ->  operator exist ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-08-14 15:10 -0700
                Re: Why does the  ->  operator exist ? Phil Carmody <pc+usenet@asdf.org> - 2023-08-15 23:00 +0300
                Re: Why does the  ->  operator exist ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-16 05:29 -0700
      Re: Why does the  ->  operator exist ? Kaz Kylheku <864-117-4973@kylheku.com> - 2023-07-22 05:39 +0000
  Re: Why does the -> operator exist ? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2023-06-28 14:16 -0700

csiph-web