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


Groups > comp.lang.c > #95250

Re: pointers

From BartC <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: pointers
Date 2016-10-24 23:53 +0100
Organization A noiseless patient Spider
Message-ID <num3c9$jso$1@dont-email.me> (permalink)
References (9 earlier) <lneg35bsfq.fsf@kst-u.example.com> <nulfjf$qgb$1@dont-email.me> <ln60ohblkv.fsf@kst-u.example.com> <nulp42$nqg$1@dont-email.me> <lnshrla4et.fsf@kst-u.example.com>

Show all headers | View raw


On 24/10/2016 21:16, Keith Thompson wrote:
> BartC <bc@freeuk.com> writes:

>> But &**pointer yields the address of the int value designated by **pointer.
>
> Yes.
>
>> That address is itself stored in an object and can also be written as
>> *pointer.
>
> No, it's not stored in an object.  Why do you think it is?

Take int **pointer with a layout like this at the following 'addresses':

pointer: 1000:[2000]    pointer to pointer to int at 1000
          2000:[3000]    pointer to int at 2000
          3000:[7777]    int at 3000 containing value 7777


>> So &**pointer and *pointer yield the same value, the same type, and the
>> same object.

And the values resulting from various expressions:

      pointer        2000
     *pointer        3000    LVALUE according to your posts
    **pointer        7777    LVALUE "

     &pointer        1000
    &*pointer        2000
   &**pointer        3000    NOT LVALUE "
& &**pointer        2000 ?

The 3000 from *pointer and the 3000 from &**pointer are the same value 
and the same type. Neither are the actual stored 3000, but a copy of it 
used as a transient rvalue existing somewhere in the workspace of the 
computer (registers, stack etc).

One of these however can have & added in front, and the other can't.

Why? Because *pointer is apparently an Lvalue, but &**pointer isn't. And 
why isn't it? Because it is Written. Not really very convincing...

Actually I find your statements as to what is and what isn't an Lvalue 
confusing. I originally had LVALUE/NOT LVALUE above the other way around 
before I looked again at your posts.

I've always thought passing a writeable Lvalue to functions often 
involved adding in a & to an expression. Now you say I have to leave out 
the & to get the Lvalue!

> Similarly, given:
>     int n = 25;
> the expressions `n` and `25` yield the same value and the same type, but
> only one of them designates an object.

We're talking about things that might be reasonably be expected to have 
addresses; not numeric literals which usually don't.

 > Finally, I would seriously appreciate an answer to the following.
 > Exactly what problem do you have with all this?  Do you (a) not believe
 > me when I tell you what the standard actually says, or (b) not accept
 > that the standard *means* what it says, or (c) simply dislike what the
 > standard says?

No problem. I'm just intrigued. The language implementations I've done 
work differently, that's all. Here's some of the examples in my language 
(^ means dereference):

     int         r = 7777
     ref int     q = &r
     ref ref int p = &q

     println p^^
     println &p^^
     println &&p^^
     println &&&p^^

     p^^ := 8888
     println p^^

     &&&&&p^^^^^^^ := 9999
     println p^^

And here's the C generated by that:

int       r = 7777;
int *     q = &r;
int * *   p = &q;
     printf("%d\n", **p);        // 7777
     printf("%p\n", *p);
     printf("%p\n", p);
     printf("%p\n", &p);

     **p = 8888;
     printf("%d\n",**p);         // 8888

     **p = 9999;
     printf("%d\n",**p);         // 9999

Notice all & and ^ ops have been balanced out so that only the net deref 
or address-of ops make it through to the C. The idea that & and * remove 
or add derefs just sort of works.

As for a real use-case, say M is a macro accessing some element of a 
data structure. But if you want to pass a pointer to it to a function, 
then you might expect to be able to write this:

   &M

However, because of these rules, you now have to be aware of what's 
inside M, because if it happens to start with with & or (&, then it 
won't work. In my language with a more casual approach, it will.

Here's another: you want this expression:

    (a ? &b : &c);

but the & is applied twice to each internal element. Why can't you write 
this:

   &(a ? b : c);

This doesn't work; but the M example above may contain just such an 
expression. And again, in my language, this works (admittedly to my 
surprise; the & just propagates inside the expression).

So there do appear to be advantages to doing things the way I've suggested.

-- 
Bartc

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


Thread

pointers asetofsymbols@gmail.com - 2016-10-23 08:11 -0700
  Re: pointers Richard Heathfield <rjh@cpax.org.uk> - 2016-10-23 16:26 +0100
    Re: pointers asetofsymbols@gmail.com - 2016-10-23 10:35 -0700
      Re: pointers Ike Naar <ike@iceland.freeshell.org> - 2016-10-23 17:47 +0000
        Re: pointers asetofsymbols@gmail.com - 2016-10-25 12:55 -0700
      Re: pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2016-10-23 13:58 -0400
        Re: pointers BartC <bc@freeuk.com> - 2016-10-24 00:55 +0100
          Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-23 17:27 -0700
            Re: pointers BartC <bc@freeuk.com> - 2016-10-24 11:53 +0100
              Re: pointers supercat@casperkitty.com - 2016-10-24 07:29 -0700
                Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-24 08:55 -0700
                Re: pointers Tim Rentsch <txr@alumni.caltech.edu> - 2016-10-24 14:02 -0700
                Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-24 14:42 -0700
                Re: pointers Tim Rentsch <txr@alumni.caltech.edu> - 2016-10-25 08:56 -0700
                Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-25 12:17 -0700
                Re: pointers Tim Rentsch <txr@alumni.caltech.edu> - 2016-10-26 06:37 -0700
              Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-24 08:49 -0700
                Re: pointers BartC <bc@freeuk.com> - 2016-10-24 17:25 +0100
                Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-24 09:52 -0700
                Re: pointers BartC <bc@freeuk.com> - 2016-10-24 18:15 +0100
                Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-24 12:20 -0700
                Re: pointers BartC <bc@freeuk.com> - 2016-10-24 20:58 +0100
                Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-24 13:16 -0700
                Re: pointers BartC <bc@freeuk.com> - 2016-10-24 23:53 +0100
                Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-25 13:38 -0700
                Re: pointers BartC <bc@freeuk.com> - 2016-10-25 23:06 +0100
                Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-25 15:39 -0700
                Re: pointers BartC <bc@freeuk.com> - 2016-10-26 00:26 +0100
                Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-25 16:44 -0700
                Re: pointers BartC <bc@freeuk.com> - 2016-10-26 01:02 +0100
                Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-25 17:35 -0700
                Re: pointers Ike Naar <ike@iceland.freeshell.org> - 2016-10-26 06:27 +0000
                Re: pointers BartC <bc@freeuk.com> - 2016-10-26 12:47 +0100
                Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-26 09:05 -0700
                Re: pointers BartC <bc@freeuk.com> - 2016-10-26 18:46 +0100
                Re: pointers supercat@casperkitty.com - 2016-10-26 11:44 -0700
                Re: pointers BartC <bc@freeuk.com> - 2016-10-26 20:12 +0100
                Re: pointers BartC <bc@freeuk.com> - 2016-10-26 20:30 +0100
                Re: pointers supercat@casperkitty.com - 2016-10-26 13:02 -0700
                Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-26 11:52 -0700
                Re: pointers Ike Naar <ike@iceland.freeshell.org> - 2016-10-26 19:11 +0000
                Re: pointers BartC <bc@freeuk.com> - 2016-10-26 21:25 +0100
                Re: pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2016-10-25 18:56 -0400
                Re: pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2016-10-25 19:12 -0400
                Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-25 16:31 -0700
                Re: pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-10-24 21:36 +0100
                Re: pointers BartC <bc@freeuk.com> - 2016-10-25 16:54 +0100
                Re: pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-10-25 19:09 +0100
                Re: pointers supercat@casperkitty.com - 2016-10-24 11:33 -0700
                Re: pointers Keith Thompson <kst-u@mib.org> - 2016-10-24 12:22 -0700
          Re: pointers James Kuyper <jameskuyper@verizon.net> - 2016-10-24 10:23 -0400
  Re: pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2016-10-23 12:31 -0400
    Re: pointers Richard Heathfield <rjh@cpax.org.uk> - 2016-10-23 17:38 +0100
  Re: pointers Lorinczy Zsigmond <zsiga@nospam.for.me> - 2016-10-24 14:02 +0200
  Re: pointers John Bode <jfbode1029@gmail.com> - 2016-10-24 13:34 -0700

csiph-web