Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"We can't schedule an orgy, it might be construed as fighting" -- Stanley Sutton


devel / comp.lang.c / 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
on understanding & and pointer arithmetic

<86a6h0eiy9.fsf@levado.to>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!news.mixmin.net!aioe.org!pdnKqNuC1JMi8oZRH25THw.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: on understanding & and pointer arithmetic
Date: Thu, 16 Dec 2021 11:13:34 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86a6h0eiy9.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="58234"; 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:SuQoW88eV8V+korHMRIVgIx4vZ4=
 by: Meredith Montgomery - Thu, 16 Dec 2021 14:13 UTC

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.

--8<---------------cut here---------------start------------->8---
#include <stdio.h>
int main() {
char a[] = "hello, world";
printf("a: %s\n", &a[3]);
printf("a: %s\n", a + 3);
printf("a: %s\n", &a + 3);
} --8<---------------cut here---------------end--------------->8---

%./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?

I get the my-expected result this way:

--8<---------------cut here---------------start------------->8---
#include <stdio.h>
int main() {
char a[] = "hello, world";
printf("a: %s\n", &a[3]);
printf("a: %s\n", a + 3);
printf("a: %s\n", (char*) &a + 3);
} --8<---------------cut here---------------end--------------->8---

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?

My real request is --- can you educate me on this matter? I don't think
I'm looking at things in the proper way. For example,

printf("a: %s\n", (char*) (&a + 3));

produces the garbage I saw before.

Trying to see what's going on, I wrote

printf("a: %p\n", a);
printf("a: %p\n", a + 3);
printf("a: %p\n", &a + 3);

and I got

--8<---------------cut here---------------start------------->8---
%./hello.exe
a: 0xffffcc33
a: 0xffffcc36
a: 0xffffcc5a
--8<---------------cut here---------------end--------------->8---

which illustrates my misunderstanding. I expected the two last lines to
print ``a: 0xffffcc36'', but somehow ``&a + 3'' yields ``0xffffcc5a''
and not the 3 bytes further from 0xffffcc33 that I expected.

Thank you so much.

Re: on understanding & and pointer arithmetic

<spfi5g$6ko$1@dont-email.me>

  copy mid

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

  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: Thu, 16 Dec 2021 14:23:46 +0000
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <spfi5g$6ko$1@dont-email.me>
References: <86a6h0eiy9.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 16 Dec 2021 14:23:44 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3043536772e7831927a8259101779feb";
logging-data="6808"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/81IFVu7yJd0g9xDwN8ZAOuL5bhU8DYz8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.0
Cancel-Lock: sha1:8kyVFv+QjrpZkEavUeuSVjEK9HQ=
In-Reply-To: <86a6h0eiy9.fsf@levado.to>
 by: Bart - Thu, 16 Dec 2021 14:23 UTC

On 16/12/2021 14: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.

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.

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

Re: on understanding & and pointer arithmetic

<865yrod2cx.fsf@levado.to>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!pdnKqNuC1JMi8oZRH25THw.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: Thu, 16 Dec 2021 11:57:18 -0300
Organization: Aioe.org NNTP Server
Message-ID: <865yrod2cx.fsf@levado.to>
References: <86a6h0eiy9.fsf@levado.to> <spfi5g$6ko$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="36171"; posting-host="pdnKqNuC1JMi8oZRH25THw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:/UAG5qBhe8+Olovv5JfpUwpLyaU=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Thu, 16 Dec 2021 14:57 UTC

Bart <bc@freeuk.com> writes:

> On 16/12/2021 14: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.
>
> 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.

Point taken. I have been considering just an array of characters, so
can we focus on this more restricted context?

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

Oh, so that's at least part of what I didn't know. I'm trying to use
this new information to explain the numbers, but I haven't made it yet.

--8<---------------cut here---------------start------------->8---
#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);
} --8<---------------cut here---------------end--------------->8---

%./hello.exe
a: 4294954044
a: 4294954047
a: 4294954056
%

The array a is at 4294954044 and a + 3 is at 4294954047, as I expected.

Why is the third number at 4294954056? This is 9 bytes to the right of
a. Why 9 bytes? I thought it could be 3 times the array size, but the
array size is 4 bytes (there's an ending '\0' in there) and not 3. So
if that were the case, I'd answer 12 bytes to the right of /a/ and not 9
bytes.

Can you put me in the right track? Thank you.

Re: on understanding & and pointer arithmetic

<spfllt$vkn$1@dont-email.me>

  copy mid

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

  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: Thu, 16 Dec 2021 16:23:41 +0100
Organization: A noiseless patient Spider
Lines: 122
Message-ID: <spfllt$vkn$1@dont-email.me>
References: <86a6h0eiy9.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 16 Dec 2021 15:23:41 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="5c5380de63587e5edca61de9ce276e88";
logging-data="32407"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19aU7udyxgokZyNvC4EqHB03l1+bCBcJek="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:JyIL4jzrq2RicHNaZ5du9CL4pyM=
In-Reply-To: <86a6h0eiy9.fsf@levado.to>
Content-Language: en-GB
 by: David Brown - Thu, 16 Dec 2021 15:23 UTC

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.

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.

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.

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

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

> }
> --8<---------------cut here---------------end--------------->8---
>
> %./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.

>
> I get the my-expected result this way:
>
> --8<---------------cut here---------------start------------->8---
> #include <stdio.h>
> int main() {
> char a[] = "hello, world";
> printf("a: %s\n", &a[3]);
> printf("a: %s\n", a + 3);
> printf("a: %s\n", (char*) &a + 3);
> }
> --8<---------------cut here---------------end--------------->8---
>
> 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?

For a good reference, I like "en.cppreference.com". Here are some
relevant pages:

<https://en.cppreference.com/w/c/language/conversion>
<https://en.cppreference.com/w/c/language/operator_member_access>
<https://en.cppreference.com/w/c/language/array>

That site is an easier reference than reading the standards. But it is
still a reference - you may want a tutorial page rather than a reference.

>
> My real request is --- can you educate me on this matter? I don't think
> I'm looking at things in the proper way. For example,
>
> printf("a: %s\n", (char*) (&a + 3));
>
> produces the garbage I saw before.
>
> Trying to see what's going on, I wrote
>
> printf("a: %p\n", a);
> printf("a: %p\n", a + 3);
> printf("a: %p\n", &a + 3);
>
> and I got
>
> --8<---------------cut here---------------start------------->8---
> %./hello.exe
> a: 0xffffcc33
> a: 0xffffcc36
> a: 0xffffcc5a
> --8<---------------cut here---------------end--------------->8---
>
> which illustrates my misunderstanding. I expected the two last lines to
> print ``a: 0xffffcc36'', but somehow ``&a + 3'' yields ``0xffffcc5a''
> and not the 3 bytes further from 0xffffcc33 that I expected.
>
> Thank you so much.
>

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

Re: on understanding & and pointer arithmetic

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

  copy mid

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

  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: Thu, 16 Dec 2021 17:42:46 +0000
Organization: A noiseless patient Spider
Lines: 73
Message-ID: <87h7b8wind.fsf@bsb.me.uk>
References: <86a6h0eiy9.fsf@levado.to> <spfi5g$6ko$1@dont-email.me>
<865yrod2cx.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="b9f7989dd930ff4732a641e9d197fd11";
logging-data="28609"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Jv0L4Nh4rHPlzy7SO5VGKerVUhMHsAL0="
Cancel-Lock: sha1:JJV4syCsrwNYskeGoKn5x7CZBhI=
sha1:iw4P8uUnZ26mtdU36LFgc9MaPJE=
X-BSB-Auth: 1.d92e77e6dde89ab0748c.20211216174246GMT.87h7b8wind.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 16 Dec 2021 17:42 UTC

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.

> Oh, so that's at least part of what I didn't know. I'm trying to use
> this new information to explain the numbers, but I haven't made it
> yet.

Take care with Bart's replies. He loves to make things sound more
complicated than then really are, and in this case the types he wrote
are wrong. Also, pointer to array types are rarely used in C, so while
it's worth knowing about them, must people learn a lot of C before
coming across them.

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

#include <stdio.h>
int main(void) { // Empty ()s here have a special, archaic, meaning.
char a[] = "abc";
// Declare a to be an array of 4 characters initialised with
// 'a', 'b', 'c' and '\0'.
printf("a: %p\n", (void *)a);
// C has a format for pointers (%p), but you must convert the
// pointer to the expected type of void *.
printf("a: %p\n", (void *)(a + 3));
printf("a: %p\n", (void *)(&a + 3));
}

> %./hello.exe
> a: 4294954044

This is the address of a -- the address of the first element. Pointers
in C are addresses with type information.

> a: 4294954047

This is the address of the null at the end of the array. An array name,
in most contexts, converts to a pointer to the first element, so a here
is of type char *. Pointer arithmetic is done by adding units of the
size of the pointed-to type and, since sizeof(char) is 1, that adds
three.

> a: 4294954056

An array as the operand of & is one of very few cases where the
conversion to a pointer to the first element does not occur. The result
is a pointer to the whole four-byte array. Adding 3 therefore adds 3
times the size of that object, i.e. 12.

--
Ben.

Re: on understanding & and pointer arithmetic

<spfvmh$9jq$1@dont-email.me>

  copy mid

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

  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: Thu, 16 Dec 2021 18:14:42 +0000
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <spfvmh$9jq$1@dont-email.me>
References: <86a6h0eiy9.fsf@levado.to> <spfi5g$6ko$1@dont-email.me>
<865yrod2cx.fsf@levado.to> <87h7b8wind.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 16 Dec 2021 18:14:41 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3043536772e7831927a8259101779feb";
logging-data="9850"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19tzwzFl0LRWakxDCqv6hnzggkk4wJMyy8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.0
Cancel-Lock: sha1:gLeJFgPvzEmpN88zMIVF572kJCE=
In-Reply-To: <87h7b8wind.fsf@bsb.me.uk>
 by: Bart - Thu, 16 Dec 2021 18:14 UTC

On 16/12/2021 17:42, Ben Bacarisse wrote:
> 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(*)[]),

OK.

> it's still wrong because the size is missing.

When the OP wrote about a[3] at the top of the post, there was no
declaration for 'a' and therefore no size. My remark applied to an array
of any type.

And using [] generally means an array of any size.

(You can write a function taking T(*)[], and call it with T(*)[3] or
T(*)[300].)

>> Oh, so that's at least part of what I didn't know. I'm trying to use
>> this new information to explain the numbers, but I haven't made it
>> yet.
>
> Take care with Bart's replies. He loves to make things sound more
> complicated than then really are, and in this case the types he wrote
> are wrong. Also, pointer to array types are rarely used in C, so while
> it's worth knowing about them,

Well, the OP mentioned &a, and that is its type.

Re: on understanding & and pointer arithmetic

<8635msbbcu.fsf@levado.to>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!pdnKqNuC1JMi8oZRH25THw.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: Thu, 16 Dec 2021 16:25:53 -0300
Organization: Aioe.org NNTP Server
Message-ID: <8635msbbcu.fsf@levado.to>
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=
 by: Meredith Montgomery - Thu, 16 Dec 2021 19:25 UTC

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!

Re: on understanding & and pointer arithmetic

<86ee6c9w1a.fsf@levado.to>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!aioe.org!pdnKqNuC1JMi8oZRH25THw.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: Thu, 16 Dec 2021 16:42:09 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86ee6c9w1a.fsf@levado.to>
References: <86a6h0eiy9.fsf@levado.to> <spfi5g$6ko$1@dont-email.me>
<865yrod2cx.fsf@levado.to> <87h7b8wind.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="54795"; posting-host="pdnKqNuC1JMi8oZRH25THw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:VRyWnuyVM5GuKvJ6nC7FeoPVhGs=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Thu, 16 Dec 2021 19:42 UTC

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. Bart
effectively wrote (char*)[], which is not the same or perhaps not a
valid type at all. That's my understanding now.

>> Oh, so that's at least part of what I didn't know. I'm trying to use
>> this new information to explain the numbers, but I haven't made it
>> yet.
>
> Take care with Bart's replies. He loves to make things sound more
> complicated than then really are, and in this case the types he wrote
> are wrong. Also, pointer to array types are rarely used in C, so while
> it's worth knowing about them, must people learn a lot of C before
> coming across them.

Thanks for the advice. I'm still going to study declarations. I do
think they're pretty difficult, but I believe they don't appear too
often. Brian Kernighan says the same in the book.

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

> #include <stdio.h>
> int main(void) { // Empty ()s here have a special, archaic, meaning.
> char a[] = "abc";
> // Declare a to be an array of 4 characters initialised with
> // 'a', 'b', 'c' and '\0'.
> printf("a: %p\n", (void *)a);
> // C has a format for pointers (%p), but you must convert the
> // pointer to the expected type of void *.
> printf("a: %p\n", (void *)(a + 3));
> printf("a: %p\n", (void *)(&a + 3));
> }
>
>> %./hello.exe
>> a: 4294954044
>
> This is the address of a -- the address of the first element. Pointers
> in C are addresses with type information.

That's good info! I must keep this in mind --- addresses with type
information. Thank you!

>> a: 4294954047
>
> This is the address of the null at the end of the array. An array name,
> in most contexts, converts to a pointer to the first element, so a here
> is of type char *. Pointer arithmetic is done by adding units of the
> size of the pointed-to type and, since sizeof(char) is 1, that adds
> three.
>
>> a: 4294954056
>
> An array as the operand of & is one of very few cases where the
> conversion to a pointer to the first element does not occur. The result
> is a pointer to the whole four-byte array. Adding 3 therefore adds 3
> times the size of that object, i.e. 12.

Thank you. That's right --- I was expecting a conversion to a pointer
to the first element. I'm much better prepared now. Thanks a lot!

Re: on understanding & and pointer arithmetic

<spg6b8$on0$1@dont-email.me>

  copy mid

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

  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: Thu, 16 Dec 2021 21:08:08 +0100
Organization: A noiseless patient Spider
Lines: 149
Message-ID: <spg6b8$on0$1@dont-email.me>
References: <86a6h0eiy9.fsf@levado.to> <spfllt$vkn$1@dont-email.me>
<8635msbbcu.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 16 Dec 2021 20:08:08 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="5c5380de63587e5edca61de9ce276e88";
logging-data="25312"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Z5Pg7WWeXhV6Pk1KwQPXS5NmGrpQ1JMw="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:j1MY0PF32d3d1ev8mkjKKXN5WQQ=
In-Reply-To: <8635msbbcu.fsf@levado.to>
Content-Language: en-GB
 by: David Brown - Thu, 16 Dec 2021 20:08 UTC

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.

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

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

Yes. Once you get used to the way array identifiers are converted into
pointers, it all works naturally and easily.

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

Yes.

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

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

Re: on understanding & and pointer arithmetic

<spg7sd$10ue$1@gioia.aioe.org>

  copy mid

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

  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: Thu, 16 Dec 2021 21:34:21 +0100
Organization: Aioe.org NNTP Server
Message-ID: <spg7sd$10ue$1@gioia.aioe.org>
References: <86a6h0eiy9.fsf@levado.to> <spfllt$vkn$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="33742"; 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 - Thu, 16 Dec 2021 20:34 UTC

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).
This is to say that attempting to access a memory location that has not
been explicitly allocated or mapped by the program is invalid in C.
Common practical results of many implementations might range from
printing garbage (as you say, whatever happens to be there), run into a
memory access violation (aka segmentation fault), or more unexpected
behaviors.

Re: on understanding & and pointer arithmetic

<87ee6c8ekk.fsf@nosuchdomain.example.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Thu, 16 Dec 2021 12:44:43 -0800
Organization: None to speak of
Lines: 31
Message-ID: <87ee6c8ekk.fsf@nosuchdomain.example.com>
References: <86a6h0eiy9.fsf@levado.to> <spfi5g$6ko$1@dont-email.me>
<865yrod2cx.fsf@levado.to> <87h7b8wind.fsf@bsb.me.uk>
<86ee6c9w1a.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="20ffafaec83dfe7e814f2bd671a6cef3";
logging-data="24065"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+NfcN8OWw6zrZelJtBp+3C"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:8xeGX5NqKSzAmiStVJUk1o7Tp1g=
sha1:MfckUsM01OFnXx/y/J503BLMEWg=
 by: Keith Thompson - Thu, 16 Dec 2021 20:44 UTC

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

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: on understanding & and pointer arithmetic

<87a6h08e4a.fsf@nosuchdomain.example.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Thu, 16 Dec 2021 12:54:29 -0800
Organization: None to speak of
Lines: 48
Message-ID: <87a6h08e4a.fsf@nosuchdomain.example.com>
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: reader02.eternal-september.org; posting-host="20ffafaec83dfe7e814f2bd671a6cef3";
logging-data="24065"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19EV2ccTn2mDmplLxUJZohE"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:yVeud7WPktUnys0XwUd4n6CEnpI=
sha1:0kgADiEzDViqtf6IxNi8yRP2J8c=
 by: Keith Thompson - Thu, 16 Dec 2021 20:54 UTC

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

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.

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.

[...]

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: on understanding & and pointer arithmetic

<8735msw8v3.fsf@bsb.me.uk>

  copy mid

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

  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: Thu, 16 Dec 2021 21:14:08 +0000
Organization: A noiseless patient Spider
Lines: 57
Message-ID: <8735msw8v3.fsf@bsb.me.uk>
References: <86a6h0eiy9.fsf@levado.to> <spfi5g$6ko$1@dont-email.me>
<865yrod2cx.fsf@levado.to> <87h7b8wind.fsf@bsb.me.uk>
<spfvmh$9jq$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="b9f7989dd930ff4732a641e9d197fd11";
logging-data="20083"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+1LR3/wpe8DPN+S+dB2CuK9y4cQYYfqXw="
Cancel-Lock: sha1:737krAwpN+k+p1Pj8bqUn63cRWg=
sha1:NQ5aB+ghCxwVR0csdVecNiawrQU=
X-BSB-Auth: 1.cf76ee07f66250606a5f.20211216211408GMT.8735msw8v3.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 16 Dec 2021 21:14 UTC

Bart <bc@freeuk.com> writes:

> On 16/12/2021 17:42, Ben Bacarisse wrote:
>> 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(*)[]),
>
> OK.
>
>> it's still wrong because the size is missing.
>
> When the OP wrote about a[3] at the top of the post, there was no
> declaration for 'a' and therefore no size. My remark applied to an
> array of any type.

That was an array access, not a declaration. There was a declaration of
'a' in the post you replied to, but the type of &a in that context would
have been char (*)[13] as the initialiser was "hello, world".

> And using [] generally means an array of any size.

It actually means the array type is incomplete. That means the array
has an unknown size. I think "an array of any size" is too vague and
sounds more "useful" than incomplete array types really are.

> (You can write a function taking T(*)[], and call it with T(*)[3] or
> T(*)[300].)

Yes, a pointer to an incomplete array type is compatible with a pointer
to an array of a known size.

>>> Oh, so that's at least part of what I didn't know. I'm trying to use
>>> this new information to explain the numbers, but I haven't made it
>>> yet.
>> Take care with Bart's replies. He loves to make things sound more
>> complicated than then really are, and in this case the types he wrote
>> are wrong. Also, pointer to array types are rarely used in C, so while
>> it's worth knowing about them,
>
> Well, the OP mentioned &a, and that is its type.

It's type was (in the post you were replying to) char (*)[13]. Giving
the wrong syntax for a pointer to an incomplete array type using "T" as
the element type was, to my, mind over-complicating the reply.

--
Ben.

Re: on understanding & and pointer arithmetic

<standard-20211216221139@ram.dialup.fu-berlin.de>

  copy mid

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

  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: 16 Dec 2021 21:15:49 GMT
Organization: Stefan Ram
Lines: 23
Expires: 1 Mar 2022 11:59:58 GMT
Message-ID: <standard-20211216221139@ram.dialup.fu-berlin.de>
References: <86a6h0eiy9.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de D/0MXdKkIEYZzCWtIqKlxAUQG01tLyNTkexVfQAib2jkLh
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 - Thu, 16 Dec 2021 21:15 UTC

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.

Re: on understanding & and pointer arithmetic

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

  copy mid

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

  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: Thu, 16 Dec 2021 21:34:26 +0000
Organization: A noiseless patient Spider
Lines: 84
Message-ID: <87wnk4utcs.fsf@bsb.me.uk>
References: <86a6h0eiy9.fsf@levado.to> <spfi5g$6ko$1@dont-email.me>
<865yrod2cx.fsf@levado.to> <87h7b8wind.fsf@bsb.me.uk>
<86ee6c9w1a.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="b9f7989dd930ff4732a641e9d197fd11";
logging-data="29240"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18qz0jLqu0G+h1MA8vVF7Ktxtj6mjPVwRU="
Cancel-Lock: sha1:Jl/RZWusZKi9hQaaHjWhq1nSK3M=
sha1:cglMnEpqk65FmIVgP8UGkxgwLJc=
X-BSB-Auth: 1.c90a4d50cbe5cb06fab9.20211216213427GMT.87wnk4utcs.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 16 Dec 2021 21:34 UTC

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

> Bart
> effectively wrote (char*)[], which is not the same or perhaps not a
> valid type at all. That's my understanding now.

Yes, that was just (I assume) a typo. Bart knows how to write pointer-
to-array types because his software uses them more than most C code
does. But the empty [] are wrong here. Your [13] is correct.

<cut>
>>> #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.)

--
Ben.

Re: on understanding & and pointer arithmetic

<875yro8afq.fsf@nosuchdomain.example.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Thu, 16 Dec 2021 14:14:01 -0800
Organization: None to speak of
Lines: 53
Message-ID: <875yro8afq.fsf@nosuchdomain.example.com>
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: reader02.eternal-september.org; posting-host="20ffafaec83dfe7e814f2bd671a6cef3";
logging-data="4691"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/xc0e2QWIIybmvdQguHziz"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:4h8UqWR2zaJvkGRAThFZbxO+cGc=
sha1:GCQ4ZzkddsJEK6VKMsbaTAlXERo=
 by: Keith Thompson - Thu, 16 Dec 2021 22:14 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:
[...]
>>>> #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.)

Yes, but the usual way to print pointer values is to cast to void* and
use "%p".

The "%p" format produces a string that represents a pointer value in
some implementation-defined manner (hexadecimal is common). (Bart
mentioned this upthread.)

You can convert to uintptr_t and use %"PRIuPTR" if (a) you specifically
want decimal output for some reason and (b) you don't mind a slight loss
of portability (it can fail for pre-C99 implementations and for rare
implementations that don't have an integer type big enough to hold a
pointer value).

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: on understanding & and pointer arithmetic

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

  copy mid

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

  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: Thu, 16 Dec 2021 22:20:23 +0000
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <87fsqsur88.fsf@bsb.me.uk>
References: <86a6h0eiy9.fsf@levado.to> <spfllt$vkn$1@dont-email.me>
<spg7sd$10ue$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="b9f7989dd930ff4732a641e9d197fd11";
logging-data="29240"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19vfwt4lLUqEOF0wl43dPsCrwalkLIINxY="
Cancel-Lock: sha1:gO5QKzeDagGL2R2hogZ8FF0iDps=
sha1:8BlAUEF6MeZZWuRmLOx8w5t1HAE=
X-BSB-Auth: 1.39a48c65a224e4181afc.20211216222023GMT.87fsqsur88.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 16 Dec 2021 22:20 UTC

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

--
Ben.

Re: on understanding & and pointer arithmetic

<spgimb$tct$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Thu, 16 Dec 2021 18:38:50 -0500
Organization: A noiseless patient Spider
Lines: 81
Message-ID: <spgimb$tct$1@dont-email.me>
References: <86a6h0eiy9.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 16 Dec 2021 23:38:52 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="bebde84fa63202b9f055b1cf1fe4dd14";
logging-data="30109"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/iEk+IloV/J+n3VVcVSAeVFD/x++9GOtM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:AjOCw2Uq2bIbGot/93IbFOVl1gg=
In-Reply-To: <86a6h0eiy9.fsf@levado.to>
Content-Language: en-US
 by: James Kuyper - Thu, 16 Dec 2021 23:38 UTC

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

Both the main rule, and two of the exceptions, comes into play in your
code below.

> 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], and according to
6.3.2.1p3, would normally convert into a pointer to the 'h' at the start
of that array. In this context, that would be a constraint violation - a
pointer cannot be used to initialize an array. However, as explained in
6.3.2.1p3, that conversion does not occur when the string literal is
being used as an initializer for an array.

> printf("a: %s\n", &a[3]);

a[3] parses as a post-fix expression, and therefore as a
unary-expression, and the right operand of unary & is required to be a
unary expression.
Therefore, &a[3] gets parsed as &(a[3]), so 'a' does get converted into
a char* pointing at it's first element, so a[3] is equivalent to *(a+3).
The '&' doesn't apply to 'a' itself (which would prevent that conversion
from occurring), but to a[3], which is not an expression of array type,
so 6.3.2.1p3 doesn't come into play. Thus, we have the equivalent of
&*(a+3). The & and * cancel each other out, so it's simply a+3.

> printf("a: %s\n", a + 3);

'a' also converts into a pointer to it's first element here.

> printf("a: %s\n", &a + 3);

However, in this case a + 3 would be an additive expression. Unary &
cannot take an additive expression as it's right operand. Therefore,
this parses as (&a)+3. &a is one of the exceptions to the rule given in
6.3.2.1p3. Therefore, 'a' does NOT get converted into a value of pointer
type, which is good, because that value is explicitly not an lvalue, and
you can't take the address of something that isn't an lvalue.

'a' itself is an lvalue, and &a therefore results in a pointer of type
char(*)[12], a pointer to an entire array of 12 chars, not just one
char. And that's the fundamental problem.

The result of adding an integer to a pointer is defined in terms of
positions in an array of the pointed-at type. The pointed-at type in
this case is char[12]. There's only one such object in that location,
which is a itself. A single object of the pointed-at type is treated,
for the purposes of this rule, as if it were the first and only object
in a single-element array of the pointed-at type, or in other words, as
if it were char[1][12]. The problem is that you are asking it to move
the pointer 3 elements ahead in that array, and that array has only one
element. Such an expression has undefined behavior.

As a practical matter, what would happen on many systems is that they
would treats 'a' as if it were the first element in array[n][12], where
n is >= 4. Thus, (&a)+3 would result in a pointer to the 4th element of
that array. However, since there is not actually any such array, it
merely refers to whatever is actually located at the place where that
fourth element would have been, if it had existed. There's no guarantee
what's in that location, but it apparently is not another copy of the
"hello, world!" array.

Re: on understanding & and pointer arithmetic

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

  copy mid

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

  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: Thu, 16 Dec 2021 23:44:26 +0000
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <87pmpwt8rp.fsf@bsb.me.uk>
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>
<875yro8afq.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="28408566c190b4dbe1f1624967533111";
logging-data="21838"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19cNRj04z4mrj1+nR8Sx98bP0LWNDCLU1E="
Cancel-Lock: sha1:uudwsuhT2BfF1E8c7m4ay6qxEac=
sha1:081SlbxT1ceGmqp1/ZSuS6tz4aE=
X-BSB-Auth: 1.65d138ac9ebea86e35d2.20211216234426GMT.87pmpwt8rp.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 16 Dec 2021 23:44 UTC

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

> 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:
> [...]
>>>>> #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.)
>
> Yes, but the usual way to print pointer values is to cast to void* and
> use "%p".

Yes, I wrote that out for the OP in my very first reply.

> You can convert to uintptr_t and use %"PRIuPTR" if (a) you specifically
> want decimal output for some reason

Later, the OP said they /did/ want to see decimal (presumably to make
the arithmetic even clearer) so I explained how to do that.

--
Ben.

Re: on understanding & and pointer arithmetic

<spgjc7$84b$1@dont-email.me>

  copy mid

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

  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: Thu, 16 Dec 2021 23:50:32 +0000
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <spgjc7$84b$1@dont-email.me>
References: <86a6h0eiy9.fsf@levado.to> <spgimb$tct$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 16 Dec 2021 23:50:31 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="4529ebeb088a50bedf53766b4b294c1f";
logging-data="8331"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19rkNv4hIxwiwbTI87LNRMkVQTjTA4bdRA="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.0
Cancel-Lock: sha1:R5MH/sL+K6WActkp59hbzMzfBjc=
In-Reply-To: <spgimb$tct$1@dont-email.me>
 by: Bart - Thu, 16 Dec 2021 23:50 UTC

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.

Re: on understanding & and pointer arithmetic

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

  copy mid

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

  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: Thu, 16 Dec 2021 23:56:00 +0000
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <87k0g4t88f.fsf@bsb.me.uk>
References: <86a6h0eiy9.fsf@levado.to> <spgimb$tct$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="28408566c190b4dbe1f1624967533111";
logging-data="21838"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+uZuRV1Sq1JUyiCJ2uC8kBnk1a4QZ0geg="
Cancel-Lock: sha1:EOya50LX/zCIIO5XoGSCrANbtcU=
sha1:ODy6RddW8xCUh1hniYRd8IL6bWE=
X-BSB-Auth: 1.3fc926956b7ed6e9d260.20211216235600GMT.87k0g4t88f.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 16 Dec 2021 23:56 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].

I think you have not looked at what the OP wrote carefully enough.

a[3] == *(a + 3) == *(3 + a) == 3[a]

but not without the *(...) part!

--
Ben.

Re: on understanding & and pointer arithmetic

<spgjsg$jml$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Thu, 16 Dec 2021 18:59:10 -0500
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <spgjsg$jml$1@dont-email.me>
References: <86a6h0eiy9.fsf@levado.to> <spfllt$vkn$1@dont-email.me>
<spg7sd$10ue$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 16 Dec 2021 23:59:12 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="bebde84fa63202b9f055b1cf1fe4dd14";
logging-data="20181"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Z4Y6fSlEbwNNlWYQxYEwEx349MCxGgD4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:luuJWpqUtQ2L9hsM+1rux4eh3Bk=
In-Reply-To: <spg7sd$10ue$1@gioia.aioe.org>
Content-Language: en-US
 by: James Kuyper - Thu, 16 Dec 2021 23:59 UTC

On 12/16/21 3:34 PM, Manfred wrote:
[re: char a[] = "hello, world!";
,,,
> 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).

There's two problems with that statement. First of all, the expression
&a+3 has undefined behavior all by itself (6.5.6p9), even if the code
made no attempt to access the memory pointed at by the result of that
expression.
Secondly, such code simply has undefined behavior. There's no need to
qualify it with "technically". Undefined behavior simply means that the
standard doesn't impose any requirements on the resulting behavior - and
it doesn't impose any requirements on this code.
Keep in mind that undefined behavior allows, among infinitely many other
things, having the code behave in precisely the manner you incorrectly
thought it was required to behave. That case comes up pretty often,
which isn't a coincidence. The false idea that there is some particular
way that such code is required to behave doesn't just come out of
nowhere - it develops in part because there are real implementations
where it happens to have that behavior.

Re: on understanding & and pointer arithmetic

<spgkam$s7k$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: on understanding & and pointer arithmetic
Date: Thu, 16 Dec 2021 19:06:44 -0500
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <spgkam$s7k$1@dont-email.me>
References: <86a6h0eiy9.fsf@levado.to> <spgimb$tct$1@dont-email.me>
<87k0g4t88f.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 17 Dec 2021 00:06:46 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="bebde84fa63202b9f055b1cf1fe4dd14";
logging-data="28916"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/aiCoX34S2Yx2amjfJn02vj9O1DZWkjLQ="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:29rw5db+Wj0YNRdkj+cEzHcnNWQ=
In-Reply-To: <87k0g4t88f.fsf@bsb.me.uk>
Content-Language: en-US
 by: James Kuyper - Fri, 17 Dec 2021 00:06 UTC

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.

Re: on understanding & and pointer arithmetic

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

  copy mid

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

  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: Fri, 17 Dec 2021 00:10:49 +0000
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <87ee6ct7jq.fsf@bsb.me.uk>
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: reader02.eternal-september.org; posting-host="28408566c190b4dbe1f1624967533111";
logging-data="21838"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18XNQOFU0nJJyd520dJP8GeWsKijn+i7Vk="
Cancel-Lock: sha1:K+RH7pv610T0wN7Ze1vrnB8swTw=
sha1:Ra9k4OrblgKCxOsTwRqowLdVqAs=
X-BSB-Auth: 1.d602e7a788414288fd29.20211217001049GMT.87ee6ct7jq.fsf@bsb.me.uk
 by: Ben Bacarisse - Fri, 17 Dec 2021 00:10 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.

The OP wrote "a[3] is the same as a + 3" and you said "That is correct".

--
Ben.

Re: on understanding & and pointer arithmetic

<sphmnl$vuo$1@dont-email.me>

  copy mid

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

  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 10:53:57 +0100
Organization: A noiseless patient Spider
Lines: 76
Message-ID: <sphmnl$vuo$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>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 17 Dec 2021 09:53:57 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2dff273a327e2cb34cf73fe517a8293e";
logging-data="32728"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Rk5HyA7wFPxZEuyJ5Ci6TtrcCBhaaaAM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:Z5/UJclwqV1D6wRISzCwhFi1cwM=
In-Reply-To: <87a6h08e4a.fsf@nosuchdomain.example.com>
Content-Language: en-GB
 by: David Brown - Fri, 17 Dec 2021 09:53 UTC

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.

Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor