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


Groups > comp.lang.c > #163875

Re: on understanding & and pointer arithmetic

Path csiph.com!aioe.org!pdnKqNuC1JMi8oZRH25THw.user.46.165.242.75.POSTED!not-for-mail
From Meredith Montgomery <mmontgomery@levado.to>
Newsgroups comp.lang.c
Subject Re: on understanding & and pointer arithmetic
Date Thu, 16 Dec 2021 16:25:53 -0300
Organization Aioe.org NNTP Server
Message-ID <8635msbbcu.fsf@levado.to> (permalink)
References <86a6h0eiy9.fsf@levado.to> <spfllt$vkn$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
Injection-Info gioia.aioe.org; logging-data="40125"; posting-host="pdnKqNuC1JMi8oZRH25THw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice Filtered by postfilter v. 0.9.2
Cancel-Lock sha1:zcpoFJVnfFhdBFG96pm/KHOmg7I=
Xref csiph.com comp.lang.c:163875

Show key headers only | View raw


David Brown <david.brown@hesbynett.no> writes:

> On 16/12/2021 15:13, Meredith Montgomery wrote:
>> I'm investigating the syntax array[index] and I'm getting surprised at
>> some places.  My intuition says that a[3] is the same as a + 3.  I also
>> know that /&a/ is the same as /a/.  I first expected the following
>> program to print ``lo, world\n'' three times, but it does not.
>
> I can see why you are confused - you are close, but a bit mixed up.
>
> "a[3]" is the same as "*(a + 3)".  The dereferencing is crucial.

Thank you!

> In the code below, "a" is an array of 13 char - and that is its type.
> When you take its address, "&a", you have a pointer to an array of 13
> char.  This will have the same /value/ as &a[0], which is a pointer to
> the first element of the array - a pointer to a char.

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.
The value of &a is the same /value/ as &a[0].  I also get that.  I'm
good here.

> In many expressions (apart from "sizeof a" or "&a"), "a" will decay to a
> pointer to its first element.  That is, the "array of char" is turned
> into a "pointer to char".  And this pointer will have the same /value/
> as &a[0], and therefore as &a.  But it is not the same thing, even when
> used in an expression - types are very important.

Alright, I got it now.  The types change, but the values are the same.
So all care must taken precisely when pointer arithmetic is used because
the types are totally determinant there.

>> --8<---------------cut here---------------start------------->8---
>> #include <stdio.h>
>> int main() {
>>   char a[] = "hello, world";
>>   printf("a: %s\n", &a[3]);
>
> &a[3] is the address of element 3 (i.e., the fourth letter) of the array
> "a".
>
>>   printf("a: %s\n", a + 3);
>
> "a" here gets converted to a  pointer to the first element, and then 3
> elements are added - giving a pointer to a char at the address of element 3.

I'm good there too.

>>   printf("a: %s\n", &a + 3);
>
> "a" here is the full array, so "&a" is a pointer to an array of 13 char.
>  Adding 3 gives a new pointer to an array of 13 char, at the address 39
> bytes higher than address of "a".  Basically, you are pretending that
> there is an array of 4 elements, each of which is itself an array of 13
> chars - with "a" being the first of those 4 elements.  Now you are
> asking to print the 4th element here, which is just whatever happens to
> be in the memory at that address.

Let me do the arithmetic and see if I get the same thing.  Since &a is a
pointer to an array of 13 char, then I would say that

  &a + 1 

should jump 13 bytes ``to the right'' because that is the size of the
array to which &a points to.  That appears to be the case.  The 39
bytes, therefore, is due to 13 * 3.

Confirming that:

--8<---------------cut here---------------start------------->8---
#include <stdio.h>
int main(void) {
  char a[] = "hello, world";
  printf("%lu\n", (unsigned long) a);
  printf("%lu\n", (unsigned long) &a);
  printf("%lu\n", (unsigned long) (&a + 1));
  printf("Jumped %lu bytes to the right\n", 
         (unsigned long) (&a + 1) - (unsigned long) &a);
  printf("%lu\n", (unsigned long) (&a + 3));
  printf("Jumped %lu bytes to the right\n", 
         (unsigned long) (&a + 3) - (unsigned long) &a);
}
--8<---------------cut here---------------end--------------->8---

%./arr
4294954035
4294954035
4294954048
Jumped 13 bytes to the right
4294954074
Jumped 39 bytes to the right
%

If that's indeed right, then things are falling into place now.  Thank
you so much, David!  As always, I'm very grateful!

By the way, I'm using (unsigned long) and %lu because I wanted the
numbers in base 10 for an easier arithmetic life.
 
>> %./hello.exe
>> a: lo, world
>> a: lo, world
>> a: ▒
>> %
>> 
>> I think part of my problem is a type-confusion I'm making.  The type of
>> a + 3 must still be (char *) while the third one must be some other
>> type?
>
> Yes.  Understanding the source of your confusion is the first step
> towards resolving it.

If I'm not there yet, I should be close because things seem to be
working as I now expect.

[...]

> I think you should be able to figure this out now, given what I wrote
> earlier.

I think that I did.  Very grateful!

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