Sujet : Re: technology discussion → does the world need a "new" C ?
De : jameskuyper (at) *nospam* alumni.caltech.edu (James Kuyper)
Groupes : comp.lang.cDate : 10. Jul 2024, 09:07:38
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v6lbvq$1qbor$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9
User-Agent : Mozilla Thunderbird
On 7/10/24 01:55, Lawrence D'Oliveiro wrote:
On Sat, 06 Jul 2024 15:23:47 -0700, Keith Thompson wrote:
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
>
On Fri, 05 Jul 2024 11:46:38 -0700, Keith Thompson wrote:
>
No, arrays are not pointers.
>
Except array indexing is designed to be indistinguishable from pointer
arithmetic.
>
No, arrays are not pointers.
Can you point out any situation where this construct
&a[b]
might be valid, but this
(a + b)
(with the same declarations of “a” and “b”) might not?
&a[b] is defined as equivalent to &*(a+b), and which in turn is
equivalent to (a+b), so of course that's not a problem. The fact that an
array is not a pointer is not visible in that context, but is visible in
several others:
#include <stdio.h>
int main(void)
{
char *pointer = "array 1";
char array[] = "array 2";
// "array 2" is an lvalue of array type, and in most contexts,
// gets implicitly converted into a pointer to the first character
// of that array, in this context it does not, and this:
// char array[] = pointer;
// would therefore be a constraint violation;
//These aren't guaranteed to be unequal, but they are allowed to
// be, and are unlikely to be equal on most implementations.
printf("%zu != %zu", sizeof array, sizeof pointer);
// Even if the types did have the same size, you can demonstrate
// that they are different using _Generic()
// The following declarations declare objects of different types.
typeof(array) newarray;
typeof(pointer) newpointer;
printf("%zu != %zu", sizeof array, sizeof pointer);
// The following two statements would be constraint violations
// if you swapped "pointer" with "array":
char **ppchar = &pointer;
char (*parray)[8] = &array;
}