Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Computer programs expand so as to fill the core available.


devel / comp.lang.c / Re: on understanding & and pointer arithmetic

SubjectAuthor
* on understanding & and pointer arithmeticMeredith Montgomery
+* Re: on understanding & and pointer arithmeticBart
|`* Re: on understanding & and pointer arithmeticMeredith Montgomery
| `* Re: on understanding & and pointer arithmeticBen Bacarisse
|  +* Re: on understanding & and pointer arithmeticBart
|  |`- Re: on understanding & and pointer arithmeticBen Bacarisse
|  `* Re: on understanding & and pointer arithmeticMeredith Montgomery
|   +* Re: on understanding & and pointer arithmeticKeith Thompson
|   |`- Re: on understanding & and pointer arithmeticMeredith Montgomery
|   `* Re: on understanding & and pointer arithmeticBen Bacarisse
|    +* Re: on understanding & and pointer arithmeticKeith Thompson
|    |`- Re: on understanding & and pointer arithmeticBen Bacarisse
|    `- Re: on understanding & and pointer arithmeticMeredith Montgomery
+* Re: on understanding & and pointer arithmeticDavid Brown
|+* Re: on understanding & and pointer arithmeticMeredith Montgomery
||`* Re: on understanding & and pointer arithmeticDavid Brown
|| +* Re: on understanding & and pointer arithmeticKeith Thompson
|| |`* Re: on understanding & and pointer arithmeticDavid Brown
|| | `* Re: on understanding & and pointer arithmeticMeredith Montgomery
|| |  +- Re: on understanding & and pointer arithmeticScott Lurndal
|| |  +* Re: on understanding & and pointer arithmeticDavid Brown
|| |  |`- Re: on understanding & and pointer arithmeticMeredith Montgomery
|| |  `- Re: on understanding & and pointer arithmeticBart
|| `- Re: on understanding & and pointer arithmeticMeredith Montgomery
|`* Re: on understanding & and pointer arithmeticManfred
| +* Re: on understanding & and pointer arithmeticBen Bacarisse
| |`* Re: on understanding & and pointer arithmeticMeredith Montgomery
| | +* Re: on understanding & and pointer arithmeticStefan Ram
| | |`- Re: on understanding & and pointer arithmeticMeredith Montgomery
| | +- Re: on understanding & and pointer arithmeticManfred
| | `- Re: on understanding & and pointer arithmeticBen Bacarisse
| `- Re: on understanding & and pointer arithmeticJames Kuyper
+* Re: on understanding & and pointer arithmeticStefan Ram
|`* Re: on understanding & and pointer arithmeticDavid Brown
| +* Re: on understanding & and pointer arithmeticManfred
| |`- Re: on understanding & and pointer arithmeticDavid Brown
| `- Re: on understanding & and pointer arithmeticMeredith Montgomery
`* Re: on understanding & and pointer arithmeticJames Kuyper
 +* Re: on understanding & and pointer arithmeticBart
 |`* Re: on understanding & and pointer arithmeticjames...@alumni.caltech.edu
 | `- Re: on understanding & and pointer arithmeticBart
 +* Re: on understanding & and pointer arithmeticBen Bacarisse
 |`* Re: on understanding & and pointer arithmeticJames Kuyper
 | +- Re: on understanding & and pointer arithmeticBen Bacarisse
 | `- Re: on understanding & and pointer arithmeticMeredith Montgomery
 `- Re: on understanding & and pointer arithmeticMeredith Montgomery

Pages:12
Re: on understanding & and pointer arithmetic

<sphnjm$hq3$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19480&group=comp.lang.c#19480

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Fri, 17 Dec 2021 11:08:53 +0100
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <sphnjm$hq3$1@dont-email.me>
References: <86a6h0eiy9.fsf@levado.to>
<standard-20211216221139@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 17 Dec 2021 10:08:54 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2dff273a327e2cb34cf73fe517a8293e";
logging-data="18243"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+lvPfvDNaSKMoARvVXNWTSVbVn3zrityo="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:YB9tlxFo1w0qpZNNGy+8sSDXiQU=
In-Reply-To: <standard-20211216221139@ram.dialup.fu-berlin.de>
Content-Language: en-GB
 by: David Brown - Fri, 17 Dec 2021 10:08 UTC

On 16/12/2021 22:15, Stefan Ram wrote:
> Meredith Montgomery <mmontgomery@levado.to> writes:
>> My intuition says that a[3] is the same as a + 3.
>
> Well, you need to know where to look it up. Intuition cannot
> tell you this. You need to grab a copy of the C standard and
> then lookup the section on those operators!
>
> Alternatively, you can also read in a /draft/ of the C standard.
> As far as I know, these drafts are published for discussion and
> are available without pay.
>
>> So I think I must correct my intuition to remember that &var does not
>> produce a type ``char *''. What does it produce? Which reference could
>> I check to see an official statement of the fact?
>
> The same: a copy of the C standard.
>
>> My real request is --- can you educate me on this matter?
>
> The best education is to get a copy of the C standard and
> then look up the operators you are interested in.
>
>

Quoted from the "Abstract" at the beginning of the C standards:

"""
A lthough this International Standard is intended to guide knowledgeable
C language programmers as well as implementors of C language translation
systems, the document itself is not designed to serve as a tutorial.
"""

When someone asks the meaning of a traffic sign, you do not point them
to a law library. When someone asks the meaning of an expression in C,
you do not point them at the standards.

Once the OP has a stronger grasp of the language and more experience
working with it, then the standards are a useful /reference/ of precise
detail. They are not designed for education.

This group has a reputation for being far too concerned about the
minutiae of the standards than giving useable help and practical advice
to people with questions about C. There is space for all kinds of
discussion here, but it is important to try to meet people's questions
at an appropriate level.

Re: on understanding & and pointer arithmetic

<spia32$re1$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19483&group=comp.lang.c#19483

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Fri, 17 Dec 2021 16:24:17 +0100
Organization: Aioe.org NNTP Server
Message-ID: <spia32$re1$1@gioia.aioe.org>
References: <86a6h0eiy9.fsf@levado.to>
<standard-20211216221139@ram.dialup.fu-berlin.de>
<sphnjm$hq3$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="28097"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Manfred - Fri, 17 Dec 2021 15:24 UTC

On 12/17/2021 11:08 AM, David Brown wrote:
> On 16/12/2021 22:15, Stefan Ram wrote:
>> Meredith Montgomery <mmontgomery@levado.to> writes:
>>> My intuition says that a[3] is the same as a + 3.
>>
>> Well, you need to know where to look it up. Intuition cannot
>> tell you this. You need to grab a copy of the C standard and
>> then lookup the section on those operators!
>>
>> Alternatively, you can also read in a /draft/ of the C standard.
>> As far as I know, these drafts are published for discussion and
>> are available without pay.
>>
>>> So I think I must correct my intuition to remember that &var does not
>>> produce a type ``char *''. What does it produce? Which reference could
>>> I check to see an official statement of the fact?
>>
>> The same: a copy of the C standard.
>>
>>> My real request is --- can you educate me on this matter?
>>
>> The best education is to get a copy of the C standard and
>> then look up the operators you are interested in.
>>
>>
>
> Quoted from the "Abstract" at the beginning of the C standards:
>
> """
> A lthough this International Standard is intended to guide knowledgeable
> C language programmers as well as implementors of C language translation
> systems, the document itself is not designed to serve as a tutorial.
> """
>
> When someone asks the meaning of a traffic sign, you do not point them
> to a law library. When someone asks the meaning of an expression in C,
> you do not point them at the standards.
>
> Once the OP has a stronger grasp of the language and more experience
> working with it, then the standards are a useful /reference/ of precise
> detail. They are not designed for education.
>
> This group has a reputation for being far too concerned about the
> minutiae of the standards than giving useable help and practical advice
> to people with questions about C. There is space for all kinds of
> discussion here, but it is important to try to meet people's questions
> at an appropriate level.

True, and it's not just about "level" (which might sound patronizing).
More properly I'd say it is about the English language and wording.
"Standardese" is notoriously unfriendly at any level.

Re: on understanding & and pointer arithmetic

<1b0bd00a-a998-426b-9fbf-4a0461e60908n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19484&group=comp.lang.c#19484

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:1a0b:: with SMTP id bk11mr1996180qkb.513.1639755256934;
Fri, 17 Dec 2021 07:34:16 -0800 (PST)
X-Received: by 2002:a37:9b4a:: with SMTP id d71mr2032410qke.319.1639755256707;
Fri, 17 Dec 2021 07:34:16 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 17 Dec 2021 07:34:16 -0800 (PST)
In-Reply-To: <spgjc7$84b$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=108.48.119.9; posting-account=Ix1u_AoAAAAILVQeRkP2ENwli-Uv6vO8
NNTP-Posting-Host: 108.48.119.9
References: <86a6h0eiy9.fsf@levado.to> <spgimb$tct$1@dont-email.me> <spgjc7$84b$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <1b0bd00a-a998-426b-9fbf-4a0461e60908n@googlegroups.com>
Subject: Re: on understanding & and pointer arithmetic
From: jameskuy...@alumni.caltech.edu (james...@alumni.caltech.edu)
Injection-Date: Fri, 17 Dec 2021 15:34:16 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 21
 by: james...@alumni.calt - Fri, 17 Dec 2021 15:34 UTC

On Thursday, December 16, 2021 at 6:50:41 PM UTC-5, Bart wrote:
> On 16/12/2021 23:38, James Kuyper wrote:
> > On 12/16/21 9:13 AM, 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
> >
> > That is correct.
> Really? It's only equivalent in certain cases.

Meredith and I both made mistakes. He should have said `(*(a+3))`. Since
he didn't say that, I should have pointed out his mistake. However, your
comment surprises me.

Were you referring to his statement as he originally wrote it? If so, what
are the "certain cases" you're referring to? `a[3]` has the type char, while
`a+3` has the type char* - it's hard for me to see how they could ever be
equivalent.

Or did you misread his statement the same way I did? If so, under what
circumstances would `a[3]` not be equivalent to `(*(a+3))`? Section
6.5.2.1p2 says:
> ... E1[E2] is identical to (*((E1)+(E2))).

Re: on understanding & and pointer arithmetic

<spigmt$io4$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19487&group=comp.lang.c#19487

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Fri, 17 Dec 2021 18:17:16 +0100
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <spigmt$io4$1@dont-email.me>
References: <86a6h0eiy9.fsf@levado.to>
<standard-20211216221139@ram.dialup.fu-berlin.de>
<sphnjm$hq3$1@dont-email.me> <spia32$re1$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 17 Dec 2021 17:17:17 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2dff273a327e2cb34cf73fe517a8293e";
logging-data="19204"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+zGnVAYoLDFG7gTtoFh/BbEezSYLgFxMg="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:TX6IUZKqWgYhjH9TOqa5f+viB/o=
In-Reply-To: <spia32$re1$1@gioia.aioe.org>
Content-Language: en-GB
 by: David Brown - Fri, 17 Dec 2021 17:17 UTC

On 17/12/2021 16:24, Manfred wrote:
> On 12/17/2021 11:08 AM, David Brown wrote:
>>
>> This group has a reputation for being far too concerned about the
>> minutiae of the standards than giving useable help and practical advice
>> to people with questions about C.  There is space for all kinds of
>> discussion here, but it is important to try to meet people's questions
>> at an appropriate level.
>
> True, and it's not just about "level" (which might sound patronizing).
> More properly I'd say it is about the English language and wording.
> "Standardese" is notoriously unfriendly at any level.

Yes indeed.

It's also important to remember that some people might not be as fluent
in English as others. While many of the non-native English speakers
here have excellent English skills, some might not. We (and that
definitely includes me) don't always write in a way that is easy to read
for those that are not as proficient. (At least no one has to try to
understand my Scottish accent in spoken English :-) )

None of this is a criticism of people who can and do discus the
standards in all their glory and complexity - that's is just as much a
part of what we do in this group. And topic drift in threads is the way
Usenet works - no one "owns" threads and expects focus on their
question. (We are not paid technical support, and we chat about what we
want.) But many of us could sometimes benefit from keeping in mind the
kind of answer that would be of most help to someone who asks a question.

Re: on understanding & and pointer arithmetic

<spiih9$vsu$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19489&group=comp.lang.c#19489

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Fri, 17 Dec 2021 17:48:26 +0000
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <spiih9$vsu$1@dont-email.me>
References: <86a6h0eiy9.fsf@levado.to> <spgimb$tct$1@dont-email.me>
<spgjc7$84b$1@dont-email.me>
<1b0bd00a-a998-426b-9fbf-4a0461e60908n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 17 Dec 2021 17:48:25 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="4529ebeb088a50bedf53766b4b294c1f";
logging-data="32670"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/FBV/lrB83PaWjS2zRmzDAYz175+1Xc/w="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.0
Cancel-Lock: sha1:c2fuOQ0Qk4b7f0Fu0/f019BnVVg=
In-Reply-To: <1b0bd00a-a998-426b-9fbf-4a0461e60908n@googlegroups.com>
 by: Bart - Fri, 17 Dec 2021 17:48 UTC

On 17/12/2021 15:34, james...@alumni.caltech.edu wrote:
> On Thursday, December 16, 2021 at 6:50:41 PM UTC-5, Bart wrote:
>> On 16/12/2021 23:38, James Kuyper wrote:
>>> On 12/16/21 9:13 AM, 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
>>>
>>> That is correct.
>> Really? It's only equivalent in certain cases.
>
> Meredith and I both made mistakes.

Well, the OP is a newbie and that is a genuine misunderstanding.

> He should have said `(*(a+3))`. Since
> he didn't say that, I should have pointed out his mistake. However, your
> comment surprises me.
>
> Were you referring to his statement as he originally wrote it? If so, what
> are the "certain cases" you're referring to? `a[3]`

I'm refering partly to his first paragraph (I'd only glanced at the rest
of the post), and partly to my original reply to that, which I will
repeat here:

-------------------------------------------------------
It's the same as *(a+3).

However if a is an array of arrays, then the array element at *(a+3)
might decay to a pointer of that array, so it could end up as the same
value as a+3, if different type:

int a[5][4];

printf("%p\n", a[3]);
printf("%p\n", *(a+3));
printf("%p\n", a+3);

These all show the same address. But change 'a' to 'int a[5]', and the
first two lines show the value, the element a[3] (some random value if
not initialised), and the last shows the address of that element.

-------------------------------------------------------

So I say that sometimes, *(a+3) can have the same vaue as a+3.

Re: on understanding & and pointer arithmetic

<86v8zm7zmd.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19512&group=comp.lang.c#19512

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!w1hPHPb/YrPziOBjHD1tGA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Sat, 18 Dec 2021 11:32:10 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86v8zm7zmd.fsf@levado.to>
References: <86a6h0eiy9.fsf@levado.to> <spfllt$vkn$1@dont-email.me>
<8635msbbcu.fsf@levado.to> <spg6b8$on0$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="34712"; posting-host="w1hPHPb/YrPziOBjHD1tGA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:idoIQDcii79l6ULcmjKGg6KaOxc=
 by: Meredith Montgomery - Sat, 18 Dec 2021 14:32 UTC

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

> On 16/12/2021 20:25, Meredith Montgomery wrote:
>> 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.
>
> /No/. "a" is of type "array of 13 char". "&a" is of type "pointer to
> array of 13 char". These are different.

Yes --- thanks. That's what I meant, but didn't write.

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

Yes, I think I will never make that mistake again because that corrected
my intuition now. I was thinking of a pointer as just a variable
holding an integer, but there is a type associated to it which totally
changes arithmetic with it. That's my new intuition. Thanks for
helping me get there --- and for checking my work in your previous post.

[...]

> Once you feel you have got the hang of this, repeat the whole thing with
> an array of "int" rather than an array of "char". That will help you
> appreciate where the scalings come in. (Note that while an "int" is 4
> bytes, or 4 chars, on your platform, it is not necessarily the case on
> other C implementations, especially for very small devices.)

I got the hang of it. Let's see. I'll write the program with my
predictions in comments --- and run it.

--8<---------------cut here---------------start------------->8---
#include <stdio.h>
int main(void) {
int a[] = {1, 2, 3};
printf("%lu\n", (unsigned long) a); // some address
printf("%lu\n", (unsigned long) &a); // same address

printf("%lu bytes\n", (unsigned long) sizeof a); // 12 bytes
printf("%lu bytes\n", (unsigned long) sizeof &a); // 8 bytes

// this will jump 12 bytes to the right
printf("%lu\n", (unsigned long) (&a + 1));
printf("Jumped %lu bytes to the right\n",
(unsigned long) (&a + 1) - (unsigned long) &a);

// this will hump 36 bytes to the right
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---

%make arrint
cc -Wall -x c -g -std=c99 -pedantic-errors arrint.c -o arrint

%./arrint.exe
4294954036
4294954036
12 bytes
8 bytes
4294954048
Jumped 12 bytes to the right
4294954072
Jumped 36 bytes to the right
%

That looks good! Thanks very much!

Re: on understanding & and pointer arithmetic

<86czlu7zap.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19513&group=comp.lang.c#19513

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!w1hPHPb/YrPziOBjHD1tGA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Sat, 18 Dec 2021 11:39:10 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86czlu7zap.fsf@levado.to>
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> <sphmnl$vuo$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="40843"; posting-host="w1hPHPb/YrPziOBjHD1tGA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:Vkm/8C9EU9X4TPytc+pvdXUVCX8=
 by: Meredith Montgomery - Sat, 18 Dec 2021 14:39 UTC

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

> 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" :-)

For the record, I enjoyed the simplicity of this (sub)thread. I was
trying to understand why my expectation was wrong. So, I was already in
some trouble; bringing in more details at that point would not have
helped more than not bringing them in.

It's also nice to see that the situation is even deeper than I would
have thought, though. For instance, right now my intuition is that the
size of a pointer on a 64-bit machine is always 8 bytes. Can anyone
show an easy example of when this isn't true?

[...]

Re: on understanding & and pointer arithmetic

<86o85e6jo0.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19516&group=comp.lang.c#19516

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!w1hPHPb/YrPziOBjHD1tGA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Sat, 18 Dec 2021 12:02:07 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86o85e6jo0.fsf@levado.to>
References: <86a6h0eiy9.fsf@levado.to> <spfllt$vkn$1@dont-email.me>
<spg7sd$10ue$1@gioia.aioe.org> <87fsqsur88.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="59564"; posting-host="w1hPHPb/YrPziOBjHD1tGA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:XlKWk9FMbYNjubC9kWktCAo7/EE=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Sat, 18 Dec 2021 15:02 UTC

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> Manfred <noname@add.invalid> writes:
>
>> On 12/16/2021 4:23 PM, David Brown wrote:
>>> On 16/12/2021 15:13, Meredith Montgomery wrote:
>> [...]
>>>
>>>> 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.
>>>
>>
>> It might be worth mentioning that accessing memory at that address (&a
>> + 3) technically yields undefined behavior, because it is an attempt
>> to access a location outside the array (a).
>>
>
> That's a good point to make.

Indeed. Thanks. (I was aware of that. In fact, I thought that my OP
would produce a lot of --- that's undefined behavior and, so, C has
nothing to do with it. Lol. But, thankfully, you guys went straight
into helping me clear up my troubles.)

> It reminds me of a construct that I rather like:
>
> char buffer[some-complex-size];
> char *end_of_buffer = (&buffer)[1];
>
> (&buffer)[1] means *(&buffer + 1). The constructed pointer &buffer + 1
> is valid, and the * does not attempt to dereference it as *(&buffer + 1)
> is an array-valued expression. It is, instead, converted to a pointer
> to the first element of this non-existent array object -- a pointer one
> past the end of 'buffer'.

Wow, that's a cool application. Thanks for sharing.

I needed to redefine my definition of ``end''. I tend to think the end
of an array is its last byte, but agreed --- that's not quite its end
yet. It seems hard to define the exact end of something.

The end of my property should be on my property or should it be outside
of it? If *it* is outside, then because because *it* is relative to
*my* property, this *it* must be mine, so it belongs to my property. So
it's not outside of it. :-)

I would have done

char *end_of_buffer = (&buffer)[1] - 1;

Re: on understanding & and pointer arithmetic

<86h7b66jh9.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19517&group=comp.lang.c#19517

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!w1hPHPb/YrPziOBjHD1tGA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Sat, 18 Dec 2021 12:06:10 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86h7b66jh9.fsf@levado.to>
References: <86a6h0eiy9.fsf@levado.to> <spfi5g$6ko$1@dont-email.me>
<865yrod2cx.fsf@levado.to> <87h7b8wind.fsf@bsb.me.uk>
<86ee6c9w1a.fsf@levado.to> <87ee6c8ekk.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="59564"; posting-host="w1hPHPb/YrPziOBjHD1tGA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:Fv7R8Lg4B3ncR3z/qlgQAfx34yg=
 by: Meredith Montgomery - Sat, 18 Dec 2021 15:06 UTC

Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

> Meredith Montgomery <mmontgomery@levado.to> writes:
> [...]
>> Thanks for the correction. I'll repeat what you say in my words just to
>> get a chance that you might verify what I'm saying. When I have an
>> array such as
>>
>> char a[] = "hello, world"
>>
>> which occupies 13 bytes, then a pointer to it is a pointer of type
>>
>> char (*a)[13]
>>
>> and that's how we write its type --- although I could omit the letter a
>> there, perhaps I should, I also think it's okay to write it there. Bart
>> effectively wrote (char*)[], which is not the same or perhaps not a
>> valid type at all. That's my understanding now.
> [...]
>
> The name "a" is not something you can arbitrarily decide to include or
> omit.
>
> `char (*)[13]` is a type name (pointer to array of 13 chars).
>
> `char (*a)[13];` not a type name. It's a declaration. It declares an
> object named `a` of type `char (*)[13]`. (And it needs that trailing
> semicolon.)

Understood. Thanks!

Re: on understanding & and pointer arithmetic

<867dc26jce.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19518&group=comp.lang.c#19518

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!w1hPHPb/YrPziOBjHD1tGA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Sat, 18 Dec 2021 12:09:05 -0300
Organization: Aioe.org NNTP Server
Message-ID: <867dc26jce.fsf@levado.to>
References: <86a6h0eiy9.fsf@levado.to> <spfi5g$6ko$1@dont-email.me>
<865yrod2cx.fsf@levado.to> <87h7b8wind.fsf@bsb.me.uk>
<86ee6c9w1a.fsf@levado.to> <87wnk4utcs.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="59564"; posting-host="w1hPHPb/YrPziOBjHD1tGA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:Lxs7ljd1UPTO4riqJIOjkZ4Xmnc=
 by: Meredith Montgomery - Sat, 18 Dec 2021 15:09 UTC

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> Meredith Montgomery <mmontgomery@levado.to> writes:
>
>> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>
>>> Meredith Montgomery <mmontgomery@levado.to> writes:
>>>
>>>> Bart <bc@freeuk.com> writes:
>>>>> On 16/12/2021 14:13, Meredith Montgomery wrote:
>>>
>>>>>> I also
>>>>>> know that /&a/ is the same as /a/.
>>>>>
>>>>> The same /what/? Those have the same values but different types
>>>>> ((T*)[] vs T* where T is a's element type).
>>>
>>> In this specific case, char (*)[4] and char *. (T*)[] is not a valid
>>> type (for some base type T), and even with the syntax corrected (it's
>>> T(*)[]), it's still wrong because the size is missing. C does have
>>> array types with no size, but none are relevant to your case.
>>
>> Thanks for the correction. I'll repeat what you say in my words just to
>> get a chance that you might verify what I'm saying. When I have an
>> array such as
>>
>> char a[] = "hello, world"
>>
>> which occupies 13 bytes, then a pointer to it is a pointer of type
>>
>> char (*a)[13]
>>
>> and that's how we write its type --- although I could omit the letter a
>> there, perhaps I should, I also think it's okay to write it there.
>
> Not quite. When you write a type (for example, to use in a cast
> operator) you must omit the name. If you include a name, then what you
> are writing a declaration, not a type.
>
> So, the object `a` has type char [13]. When used in a most contexts,
> `a` is converted to an expression of type char *, but when you apply the
> & operator, this conversion is not done and the pointer you get is of
> type char (*)[13].

Understood.

[...]

>>>> #include <stdio.h>
>>>> int main() {
>>>> char a[] = "abc";
>>>> printf("a: %lu\n", a);
>>>> printf("a: %lu\n", a + 3);
>>>> printf("a: %lu\n", &a + 3);
>>>> }
>>>
>>> With any luck, you compiler should have had lots to say about this
>>> code. If it did not, turn up the warnings. I'll write it my way with
>>> comments.
>>
>> Yes, the warnings were there. In my new experiment I added casts, but
>> kept the %lu because I wanted to see things in base 10.
>
> OK, so you converted the pointers to unsigned long. That will often
> work, but "modern" C (since 1999), has an integer type that, if the
> conversion is possible at all, is guaranteed to be the right width:
> uintptr_t. It, along with a whole lot of other useful types, is
> declared in <stdint.h>.
>
> You might ask, how does one print such a type? Well there is another
> header, <inttypes.h>, that defines macros with the right width:
>
> printf("a is at %"PRIuPTR"\n", (uintptr_t)a);
>
> A bit of a mouthful, but the result will work on any system where
> pointers can be converted to some unsigned integer type. (There is also
> a signed integer type intptr_t and a corresponding macro, PRIiPTR, for
> printing it.)

Thanks. Not long ago, I think I myself initiated some thread where I
learned about these PRI* constants for printf and where to find
documentation for them. But the uintptr_t is new to me. Thanks for the
info.

Re: on understanding & and pointer arithmetic

<86wnk254ny.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19520&group=comp.lang.c#19520

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!w1hPHPb/YrPziOBjHD1tGA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Sat, 18 Dec 2021 12:11:29 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86wnk254ny.fsf@levado.to>
References: <86a6h0eiy9.fsf@levado.to>
<standard-20211216221139@ram.dialup.fu-berlin.de>
<sphnjm$hq3$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="59564"; posting-host="w1hPHPb/YrPziOBjHD1tGA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:ZIfJuInG+Q8TDKCzYOjGzLensSA=
 by: Meredith Montgomery - Sat, 18 Dec 2021 15:11 UTC

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

> On 16/12/2021 22:15, Stefan Ram wrote:

[...]

>> The best education is to get a copy of the C standard and
>> then look up the operators you are interested in.
>
> Quoted from the "Abstract" at the beginning of the C standards:
>
> """
> A lthough this International Standard is intended to guide knowledgeable
> C language programmers as well as implementors of C language translation
> systems, the document itself is not designed to serve as a tutorial.
> """
>
> When someone asks the meaning of a traffic sign, you do not point them
> to a law library. When someone asks the meaning of an expression in C,
> you do not point them at the standards.
>
> Once the OP has a stronger grasp of the language and more experience
> working with it, then the standards are a useful /reference/ of precise
> detail. They are not designed for education.
>
> This group has a reputation for being far too concerned about the
> minutiae of the standards than giving useable help and practical advice
> to people with questions about C. There is space for all kinds of
> discussion here, but it is important to try to meet people's questions
> at an appropriate level.

``A man after my own heart.''

Re: on understanding & and pointer arithmetic

<end-20211218161410@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19521&group=comp.lang.c#19521

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: 18 Dec 2021 15:14:33 GMT
Organization: Stefan Ram
Lines: 24
Expires: 1 Mar 2022 11:59:58 GMT
Message-ID: <end-20211218161410@ram.dialup.fu-berlin.de>
References: <86a6h0eiy9.fsf@levado.to> <spfllt$vkn$1@dont-email.me> <spg7sd$10ue$1@gioia.aioe.org> <87fsqsur88.fsf@bsb.me.uk> <86o85e6jo0.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de B/ZCFDw1NSfC92Y7ZZz41wCr2+12XZZ/a75cUrWyR1a2uD
X-Copyright: (C) Copyright 2021 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Sat, 18 Dec 2021 15:14 UTC

Meredith Montgomery <mmontgomery@levado.to> writes:
>of an array is its last byte, but agreed --- that's not quite its end
>yet. It seems hard to define the exact end of something.

I have developed my personal terminology for integer ranges:

For example, take the range { 3, 4, 5, 6, 7 }.

The "begin of the range" is the first number of the range,
i.e., 3.

The "bottom of the range" is the number before the first
number of the range, i.e., 2.

The "end of the range" is the last number of the range,
i.e., 7.

The "top of the range" is the number after the last number
of the range, i.e., 8.

In the standard library of C++, they call "end" what I
call "top".

Re: on understanding & and pointer arithmetic

<86o85e53sz.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19522&group=comp.lang.c#19522

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!w1hPHPb/YrPziOBjHD1tGA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Sat, 18 Dec 2021 12:30:04 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86o85e53sz.fsf@levado.to>
References: <86a6h0eiy9.fsf@levado.to> <spgimb$tct$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="18305"; posting-host="w1hPHPb/YrPziOBjHD1tGA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:8o8y9Ot+0DhmJc66fxKKzFVjWxs=
 by: Meredith Montgomery - Sat, 18 Dec 2021 15:30 UTC

James Kuyper <jameskuyper@alumni.caltech.edu> writes:

> On 12/16/21 9:13 AM, 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
>
> That is correct. Interestingly, since a + 3 == 3 + a, it is also the
> same as 3[a].
>
>> know that /&a/ is the same as /a/. I first expected the following
>
> That is incorrect. The relevant rule is
>
> "Except when it is the operand of the sizeof operator, or the unary &
> operator, or is a string literal used to initialize an array, an
> expression that has type "array of type" is converted to an expression
> with type "pointer to type" that points to the initial element of the
> array object and is not an lvalue." (6.3.2.1p3).

Thanks for the citation and the technical analysis. Much appreciated.

[...]

>> program to print ``lo, world\n'' three times, but it does not.
>>
>> --8<---------------cut here---------------start------------->8---
>> #include <stdio.h>
>> int main() {
>> char a[] = "hello, world";
>
> "hello, world" is an expression of type char[12], [...]

You must have skipped a letter while counting. With '\0' at the end, we
get char[13], but that did not complicated the reading in any way.

[...]

Re: on understanding & and pointer arithmetic

<86fsqq53p9.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19523&group=comp.lang.c#19523

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!w1hPHPb/YrPziOBjHD1tGA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Sat, 18 Dec 2021 12:32:18 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86fsqq53p9.fsf@levado.to>
References: <86a6h0eiy9.fsf@levado.to> <spgimb$tct$1@dont-email.me>
<87k0g4t88f.fsf@bsb.me.uk> <spgkam$s7k$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="18305"; posting-host="w1hPHPb/YrPziOBjHD1tGA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:r7fnDQyjk5bh+Du76vVRpPq0zYQ=
 by: Meredith Montgomery - Sat, 18 Dec 2021 15:32 UTC

James Kuyper <jameskuyper@alumni.caltech.edu> writes:

> On 12/16/21 6:56 PM, Ben Bacarisse wrote:
>> James Kuyper <jameskuyper@alumni.caltech.edu> writes:
>>
>>> On 12/16/21 9:13 AM, 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
>>>
>>> That is correct. Interestingly, since a + 3 == 3 + a, it is also the
>>> same as 3[a].
>>
>> I think you have not looked at what the OP wrote carefully enough.
>
> I did look closely enough.
>
>> a[3] == *(a + 3) == *(3 + a) == 3[a]
>>
>> but not without the *(...) part!
>
> I just forgot to put in the '*'. I also miscounted the length of the
> array, which I consider to be even more embarrassing.

I assumed you just skipped a letter, which could happen to anyone.

Re: on understanding & and pointer arithmetic

<867dc253mb.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19524&group=comp.lang.c#19524

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!w1hPHPb/YrPziOBjHD1tGA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Sat, 18 Dec 2021 12:34:04 -0300
Organization: Aioe.org NNTP Server
Message-ID: <867dc253mb.fsf@levado.to>
References: <86a6h0eiy9.fsf@levado.to> <spfllt$vkn$1@dont-email.me>
<spg7sd$10ue$1@gioia.aioe.org> <87fsqsur88.fsf@bsb.me.uk>
<86o85e6jo0.fsf@levado.to>
<end-20211218161410@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="18305"; posting-host="w1hPHPb/YrPziOBjHD1tGA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:yDQiK9YCIeqVd1YuIJiCWrBcGI0=
 by: Meredith Montgomery - Sat, 18 Dec 2021 15:34 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:

> Meredith Montgomery <mmontgomery@levado.to> writes:
>>of an array is its last byte, but agreed --- that's not quite its end
>>yet. It seems hard to define the exact end of something.
>
> I have developed my personal terminology for integer ranges:
>
> For example, take the range { 3, 4, 5, 6, 7 }.
>
> The "begin of the range" is the first number of the range,
> i.e., 3.
>
> The "bottom of the range" is the number before the first
> number of the range, i.e., 2.
>
> The "end of the range" is the last number of the range,
> i.e., 7.
>
> The "top of the range" is the number after the last number
> of the range, i.e., 8.
>
> In the standard library of C++, they call "end" what I
> call "top".

That's interesting. In technical context, it's nice to have a language
that reflects distinctions where there is.

Re: on understanding & and pointer arithmetic

<g1ovJ.110305$lz3.67826@fx34.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19528&group=comp.lang.c#19528

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.de!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx34.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: on understanding & and pointer arithmetic
Newsgroups: comp.lang.c
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> <sphmnl$vuo$1@dont-email.me> <86czlu7zap.fsf@levado.to>
Lines: 40
Message-ID: <g1ovJ.110305$lz3.67826@fx34.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Sat, 18 Dec 2021 16:31:08 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Sat, 18 Dec 2021 16:31:08 GMT
X-Received-Bytes: 2933
 by: Scott Lurndal - Sat, 18 Dec 2021 16:31 UTC

Meredith Montgomery <mmontgomery@levado.to> writes:
>David Brown <david.brown@hesbynett.no> writes:
>

>>
>> 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" :-)
>
[snip]
>It's also nice to see that the situation is even deeper than I would
>have thought, though. For instance, right now my intuition is that the
>size of a pointer on a 64-bit machine is always 8 bytes. Can anyone
>show an easy example of when this isn't true?

That statement may be considered true for the current generation
of general purpose hardware, albeit dependent upon your definition
of 64-bit machine as supporting a 64-bit address on its bus/mesh/ring.

A system may support 64-bit arithmetic natively without supporing
more than a 32-bit address space, particularly in harvard-style
architectures.

There are systems under design right now that have 128-bit pointers,
yet only support a 64-bit address space (i.e the maximum address that
can be presented to a 'Load' or 'Store' instruction); these larger
pointers can contain 64-bits of metadata (e.g. the size of the region
covered by the pointer and access controls for reads vs. writes)
allowing the processor to efficiently prevent access outside the bounds of an object,
or to prevent stores to a read-only object.

https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/cheri-faq.html

Re: on understanding & and pointer arithmetic

<spl66f$1t5v$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19533&group=comp.lang.c#19533

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Sat, 18 Dec 2021 18:36:14 +0100
Organization: Aioe.org NNTP Server
Message-ID: <spl66f$1t5v$1@gioia.aioe.org>
References: <86a6h0eiy9.fsf@levado.to> <spfllt$vkn$1@dont-email.me>
<spg7sd$10ue$1@gioia.aioe.org> <87fsqsur88.fsf@bsb.me.uk>
<86o85e6jo0.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="62655"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Manfred - Sat, 18 Dec 2021 17:36 UTC

On 12/18/2021 4:02 PM, Meredith Montgomery wrote:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>
>> Manfred <noname@add.invalid> writes:
>>
>>> On 12/16/2021 4:23 PM, David Brown wrote:
>>>> On 16/12/2021 15:13, Meredith Montgomery wrote:
>>> [...]
>>>>
>>>>> 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.
>>>>
>>>
>>> It might be worth mentioning that accessing memory at that address (&a
>>> + 3) technically yields undefined behavior, because it is an attempt
>>> to access a location outside the array (a).
>>>
>>
>> That's a good point to make.
>
> Indeed. Thanks. (I was aware of that. In fact, I thought that my OP
> would produce a lot of --- that's undefined behavior and, so, C has
> nothing to do with it. Lol. But, thankfully, you guys went straight
> into helping me clear up my troubles.)
>
>> It reminds me of a construct that I rather like:
>>
>> char buffer[some-complex-size];
>> char *end_of_buffer = (&buffer)[1];
>>
>> (&buffer)[1] means *(&buffer + 1). The constructed pointer &buffer + 1
>> is valid, and the * does not attempt to dereference it as *(&buffer + 1)
>> is an array-valued expression. It is, instead, converted to a pointer
>> to the first element of this non-existent array object -- a pointer one
>> past the end of 'buffer'.
>
> Wow, that's a cool application. Thanks for sharing.
>
> I needed to redefine my definition of ``end''. I tend to think the end
> of an array is its last byte, but agreed --- that's not quite its end
> yet. It seems hard to define the exact end of something.
>
> The end of my property should be on my property or should it be outside
> of it? If *it* is outside, then because because *it* is relative to
> *my* property, this *it* must be mine, so it belongs to my property. So
> it's not outside of it. :-)
>
> I would have done
>
> char *end_of_buffer = (&buffer)[1] - 1;
>

It's more about habits in the context of C and how this "end" is used.

C uses the convention that indexes start at 0, which implies that a
collection of N elements has length N and is indexed from 0 to N-1.
Following the same reasoning, the following is customary in C:

#define N 42
int arr[N];
int* start = arr;
int* first = arr;
int* last = arr+N-1;
int* end = arr+N;

"end" defined this way is never dereferenced, but is typically used
(a.o.) as:

for (int* p = start; p != end; ++p)
{ *p = whatever;
}

Writing the same loop using "last" is also possible, but requires using
"<=" with pointers, which involves additional ordering requirements.

The standard follows the same habit, but is more accurate: it usually
refers to the boundary of a collection as "one past the end" of it.
However, it is this "one past the end" that is more often used rather
than the "last element" of the collection.
In fact, the "one past the end" pointer gets special attention from the
standard for the very purpose of making code like the above legal.

Re: on understanding & and pointer arithmetic

<spl7cj$3kl$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19535&group=comp.lang.c#19535

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Sat, 18 Dec 2021 18:56:35 +0100
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <spl7cj$3kl$1@dont-email.me>
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> <sphmnl$vuo$1@dont-email.me>
<86czlu7zap.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 18 Dec 2021 17:56:35 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="cda1128b60b8075ff749aba4d5123070";
logging-data="3733"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19pVIzrj0xYTNc5T7JW6aw2nMyHG6gfV5s="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:TYcmWXkVTv2WCe1JqO1+DDQVn10=
In-Reply-To: <86czlu7zap.fsf@levado.to>
Content-Language: en-GB
 by: David Brown - Sat, 18 Dec 2021 17:56 UTC

On 18/12/2021 15:39, Meredith Montgomery wrote:

>
> It's also nice to see that the situation is even deeper than I would
> have thought, though. For instance, right now my intuition is that the
> size of a pointer on a 64-bit machine is always 8 bytes. Can anyone
> show an easy example of when this isn't true?
>

There is the x32 ABI, which is an alternative ABI for 64-bit x86 systems
that uses 32-bit pointers but 64-bit integer registers and the
additional features and registers that x86-64 provides beyond x86-32.
The result was more efficient for some kinds of code, but it never
really took off. Some Linux distributions provide support for it (such
as libraries) alongside their main 64-bit stuff.

There are also a few systems, mostly historic and/or academic, that have
wider pointers containing security or protection information in addition
to addresses.

It is usually in smaller systems and nice areas (like DSPs) that you
have unusual sizes. For example, gcc for the the 8-bit AVR
microcontroller family has 16-bit int, 16-bit pointers for data and code
that are separate address spaces (i.e., a data pointer and a function
pointer could contain the same "value" if you were to cast them to a
uintptr_t, yet, point to completely different memory locations). It
also has 24-bit code pointers, and 24-bit generic pointers as well.
This is the best example I know of for strangely sized and incompatible
pointers, in devices that are popular and modern.

Re: on understanding & and pointer arithmetic

<spl99d$gne$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19539&group=comp.lang.c#19539

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Sat, 18 Dec 2021 18:29:00 +0000
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <spl99d$gne$1@dont-email.me>
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> <sphmnl$vuo$1@dont-email.me>
<86czlu7zap.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 18 Dec 2021 18:29:01 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="26bf0af5e5e6395621b07f6cdc1a725f";
logging-data="17134"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19vddhlHkOr5fC5AZ+imggz4dlaXCY6SN8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.0
Cancel-Lock: sha1:X7OkxViax55kwQrNFet3uYrp7xo=
In-Reply-To: <86czlu7zap.fsf@levado.to>
 by: Bart - Sat, 18 Dec 2021 18:29 UTC

On 18/12/2021 14:39, Meredith Montgomery wrote:
> David Brown <david.brown@hesbynett.no> writes:

>> 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" :-)
>
> For the record, I enjoyed the simplicity of this (sub)thread. I was
> trying to understand why my expectation was wrong. So, I was already in
> some trouble; bringing in more details at that point would not have
> helped more than not bringing them in.
>
> It's also nice to see that the situation is even deeper than I would
> have thought, though. For instance, right now my intuition is that the
> size of a pointer on a 64-bit machine is always 8 bytes. Can anyone
> show an easy example of when this isn't true?

C running on a typical 64-bit desktop PC will have sizeof(int) as 4
bytes, and sizeof(void*) as 8 bytes.

Unless you tell it to use a 32-bit target (eg. using -m32 option), when
pointers will be 4 bytes.

There might be a few odd implementations where it's different (for
example I've implemented a language with 32-bit pointers even in 64-bit
mode), but I wouldn't worry about that.

Re: on understanding & and pointer arithmetic

<87a6gxr4sf.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19542&group=comp.lang.c#19542

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Sat, 18 Dec 2021 21:17:52 +0000
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <87a6gxr4sf.fsf@bsb.me.uk>
References: <86a6h0eiy9.fsf@levado.to> <spfllt$vkn$1@dont-email.me>
<spg7sd$10ue$1@gioia.aioe.org> <87fsqsur88.fsf@bsb.me.uk>
<86o85e6jo0.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="24d39d537cd44dc139dae959883222c2";
logging-data="12832"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+GnxYdMxWFAXA6KOt9saBxi6Yk0FAPobU="
Cancel-Lock: sha1:Fq6ucsMYWvIYnEAXOxqeTWU9noA=
sha1:nAKs0Rsdt+oLKmwXcPAjeDPJI2o=
X-BSB-Auth: 1.2cec139f1a46165ea9cf.20211218211752GMT.87a6gxr4sf.fsf@bsb.me.uk
 by: Ben Bacarisse - Sat, 18 Dec 2021 21:17 UTC

Meredith Montgomery <mmontgomery@levado.to> writes:

> I needed to redefine my definition of ``end''. I tend to think the end
> of an array is its last byte, but agreed --- that's not quite its end
> yet. It seems hard to define the exact end of something.

C permits, for largely historical reasons, the construction of a pointer
"just past" the end of an object. As a result, you can process an array
(or some part of an array) with a conventional idiom:

for (char *ptr = start; ptr < end; ptr++) ...

which parallels the method using indexes:

for (int i = 0; i < n; i++) ...

You can get the index from the pointer (start - ptr) and the pointer
form the index (start + i) and everything works provided you don't
access the data at the "just past" address.

> The end of my property should be on my property or should it be outside
> of it? If *it* is outside, then because because *it* is relative to
> *my* property, this *it* must be mine, so it belongs to my property. So
> it's not outside of it. :-)

It's handy that the start and the end are not the same pointer unless
the range is zero. When you get into the habit of using the "just past"
pointer you will find all sorts of little things drop into place.

--
Ben.

Re: on understanding & and pointer arithmetic

<8635mg2opw.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19644&group=comp.lang.c#19644

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Q29r0ETj/jjLPjDuGSMcjw.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Sat, 25 Dec 2021 21:29:15 -0300
Organization: Aioe.org NNTP Server
Message-ID: <8635mg2opw.fsf@levado.to>
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> <sphmnl$vuo$1@dont-email.me>
<86czlu7zap.fsf@levado.to> <spl7cj$3kl$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="14411"; posting-host="Q29r0ETj/jjLPjDuGSMcjw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:t4MECghWVkobwibTgJ+AoqGjrJ8=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Sun, 26 Dec 2021 00:29 UTC

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

> On 18/12/2021 15:39, Meredith Montgomery wrote:
>
>>
>> It's also nice to see that the situation is even deeper than I would
>> have thought, though. For instance, right now my intuition is that the
>> size of a pointer on a 64-bit machine is always 8 bytes. Can anyone
>> show an easy example of when this isn't true?
>>
>
> There is the x32 ABI, which is an alternative ABI for 64-bit x86 systems
> that uses 32-bit pointers but 64-bit integer registers and the
> additional features and registers that x86-64 provides beyond x86-32.
> The result was more efficient for some kinds of code, but it never
> really took off. Some Linux distributions provide support for it (such
> as libraries) alongside their main 64-bit stuff.
>
> There are also a few systems, mostly historic and/or academic, that have
> wider pointers containing security or protection information in addition
> to addresses.
>
> It is usually in smaller systems and nice areas (like DSPs) that you
> have unusual sizes. For example, gcc for the the 8-bit AVR
> microcontroller family has 16-bit int, 16-bit pointers for data and code
> that are separate address spaces (i.e., a data pointer and a function
> pointer could contain the same "value" if you were to cast them to a
> uintptr_t, yet, point to completely different memory locations). It
> also has 24-bit code pointers, and 24-bit generic pointers as well.
> This is the best example I know of for strangely sized and incompatible
> pointers, in devices that are popular and modern.

Nice to know. Thank you and everyone else that also contributed.

Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor