Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #163891
| From | James Kuyper <jameskuyper@alumni.caltech.edu> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: on understanding & and pointer arithmetic |
| Date | 2021-12-16 18:38 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <spgimb$tct$1@dont-email.me> (permalink) |
| References | <86a6h0eiy9.fsf@levado.to> |
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.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-16 11:13 -0300
Re: on understanding & and pointer arithmetic Bart <bc@freeuk.com> - 2021-12-16 14:23 +0000
Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-16 11:57 -0300
Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 17:42 +0000
Re: on understanding & and pointer arithmetic Bart <bc@freeuk.com> - 2021-12-16 18:14 +0000
Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 21:14 +0000
Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-16 16:42 -0300
Re: on understanding & and pointer arithmetic Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-16 12:44 -0800
Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:06 -0300
Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 21:34 +0000
Re: on understanding & and pointer arithmetic Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-16 14:14 -0800
Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 23:44 +0000
Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:09 -0300
Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-16 16:23 +0100
Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-16 16:25 -0300
Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-16 21:08 +0100
Re: on understanding & and pointer arithmetic Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-16 12:54 -0800
Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-17 10:53 +0100
Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 11:39 -0300
Re: on understanding & and pointer arithmetic scott@slp53.sl.home (Scott Lurndal) - 2021-12-18 16:31 +0000
Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-18 18:56 +0100
Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-25 21:29 -0300
Re: on understanding & and pointer arithmetic Bart <bc@freeuk.com> - 2021-12-18 18:29 +0000
Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 11:32 -0300
Re: on understanding & and pointer arithmetic Manfred <noname@add.invalid> - 2021-12-16 21:34 +0100
Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 22:20 +0000
Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:02 -0300
Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:34 -0300
Re: on understanding & and pointer arithmetic Manfred <noname@add.invalid> - 2021-12-18 18:36 +0100
Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-18 21:17 +0000
Re: on understanding & and pointer arithmetic James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-12-16 18:59 -0500
Re: on understanding & and pointer arithmetic James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-12-16 18:38 -0500
Re: on understanding & and pointer arithmetic Bart <bc@freeuk.com> - 2021-12-16 23:50 +0000
Re: on understanding & and pointer arithmetic "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2021-12-17 07:34 -0800
Re: on understanding & and pointer arithmetic Bart <bc@freeuk.com> - 2021-12-17 17:48 +0000
Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-16 23:56 +0000
Re: on understanding & and pointer arithmetic James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-12-16 19:06 -0500
Re: on understanding & and pointer arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-17 00:10 +0000
Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:32 -0300
Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:30 -0300
Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-17 11:08 +0100
Re: on understanding & and pointer arithmetic Manfred <noname@add.invalid> - 2021-12-17 16:24 +0100
Re: on understanding & and pointer arithmetic David Brown <david.brown@hesbynett.no> - 2021-12-17 18:17 +0100
Re: on understanding & and pointer arithmetic Meredith Montgomery <mmontgomery@levado.to> - 2021-12-18 12:11 -0300
csiph-web