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


Groups > comp.lang.c > #163907

Re: on understanding & and pointer arithmetic

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: on understanding & and pointer arithmetic
Date 2021-12-17 10:53 +0100
Organization A noiseless patient Spider
Message-ID <sphmnl$vuo$1@dont-email.me> (permalink)
References <86a6h0eiy9.fsf@levado.to> <spfllt$vkn$1@dont-email.me> <8635msbbcu.fsf@levado.to> <spg6b8$on0$1@dont-email.me> <87a6h08e4a.fsf@nosuchdomain.example.com>

Show all headers | View raw


On 16/12/2021 21:54, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 16/12/2021 20:25, Meredith Montgomery wrote:
> [...]
>>> Yes, so that corrects me at least once.  I didn't think &a would be of a
>>> different type.  So the type of &a is array of 13 char.  I get that.
>>
>> /No/.  "a" is of type "array of 13 char".  "&a" is of type "pointer to
>> array of 13 char".  These are different.
>>
>>> The value of &a is the same /value/ as &a[0].  I also get that.  I'm
>>> good here.
>>
>> Correct.  As Ben said (and Ben is very good at explaining things
>> accurately), a pointer has a type and has an address as it's value.  So
>> the values of pointers here are the same, but the pointers themselves
>> are different because they have different types.
> 
> I don't think I'd say that &a and &a[0] have the same *value*, though it
> depends on just what you mean by "value".

Yes, indeed it does.  As always in threads like these, there is a
balance to be found between trying to have approximate explanations and
terms that are easier for the relative beginner OP, and trying to be as
accurate as possible with precise terms to help the OP get the details
right from the start (and of course there are always other people
reading the thread, who might benefit from more insight).

Objects in C always have a type, and they contain some sort of data -
which can reasonably be called its "value".  For pointers, that will be
the address they contain - the value the OP is getting when he casts
these to an unsigned long and prints them out.

I know the real picture can be more complicated - different pointer
types can have different sizes, pointers can contain more than just an
address or memory location, two pointers of different types might
contain the same raw value but refer to different address spaces, they
might contain different raw values but refer to the same location and
may or may not compare equal, there can be trap representations, etc.
(That list is not complete.)  So what I am writing is "lies to
children", rather than trying to be complete and precise (partly because
others here, such as yourself, are significantly better at that kind of
answer).  If you are not familiar with the specific phrase "lies to
children", ask Santa for "The Science of the Discworld" :-)

> 
> They refer to the same location in memory, so that `(void*)&a` and
> `(void*)&a[0]` do have the same value.  But they're of different types,
> and a "value" in C includes its type.  This can get a bit vague at
> times, and it's common to handwave away the difference, even in the
> standard.  It can be particularly vague when were talking about numeric
> values; are 3, 3U, and 3.0 the same value?
> 
> Note that `&a == &a[0]` is a constraint violation, in case you were
> thinking that "same value" can be defined in terms of equality.
> 
> For pointers of different types, I suggest avoiding the issue by saying
> that they refer to the same location.

That might have been a better way to express it.

> 
> The standard's definition of "value" (N1570 3.19) is:
> 
>     precise meaning of the contents of an object when interpreted as
>     having a specific type
> 
> That definition is incomplete, since it excludes a "value" that results
> from evaluation an expression.  `2+2` has a value (spoiler: it's 4), but
> that value is not the contents of any object.
> 

Lots of the standard's definitions can be a bit vague or incomplete, or
refer to other terms that are not defined - ultimately it is necessary
to rely on common sense or common programming concepts at some point.

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


Thread

on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-16 11:13 -0300
  Re: on understanding & and pointer arithmetic Bart <bc@freeuk.com> - 2021-12-16 14:23 +0000
    Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-16 11:57 -0300
      Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 17:42 +0000
        Re: on understanding & and pointer arithmetic Bart <bc@freeuk.com> - 2021-12-16 18:14 +0000
          Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 21:14 +0000
        Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-16 16:42 -0300
          Re: on understanding & and pointer arithmetic Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-16 12:44 -0800
            Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:06 -0300
          Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 21:34 +0000
            Re: on understanding & and pointer arithmetic Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-16 14:14 -0800
              Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 23:44 +0000
            Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:09 -0300
  Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-16 16:23 +0100
    Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-16 16:25 -0300
      Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-16 21:08 +0100
        Re: on understanding & and pointer arithmetic Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-16 12:54 -0800
          Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-17 10:53 +0100
            Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 11:39 -0300
              Re: on understanding & and pointer arithmetic scott@slp53.sl.home (Scott Lurndal) - 2021-12-18 16:31 +0000
              Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-18 18:56 +0100
                Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-25 21:29 -0300
              Re: on understanding & and pointer arithmetic Bart <bc@freeuk.com> - 2021-12-18 18:29 +0000
        Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 11:32 -0300
    Re: on understanding & and pointer arithmetic Manfred <noname@add.invalid> - 2021-12-16 21:34 +0100
      Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 22:20 +0000
        Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:02 -0300
          Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:34 -0300
          Re: on understanding & and pointer arithmetic Manfred <noname@add.invalid> - 2021-12-18 18:36 +0100
          Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-18 21:17 +0000
      Re: on understanding & and pointer arithmetic James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-12-16 18:59 -0500
  Re: on understanding & and pointer arithmetic James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-12-16 18:38 -0500
    Re: on understanding & and pointer arithmetic Bart <bc@freeuk.com> - 2021-12-16 23:50 +0000
      Re: on understanding & and pointer arithmetic "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2021-12-17 07:34 -0800
        Re: on understanding & and pointer arithmetic Bart <bc@freeuk.com> - 2021-12-17 17:48 +0000
    Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 23:56 +0000
      Re: on understanding & and pointer arithmetic James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-12-16 19:06 -0500
        Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-17 00:10 +0000
        Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:32 -0300
    Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:30 -0300
  Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-17 11:08 +0100
    Re: on understanding & and pointer arithmetic Manfred <noname@add.invalid> - 2021-12-17 16:24 +0100
      Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-17 18:17 +0100
    Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:11 -0300

csiph-web