Liste des Groupes | Revenir à cl c |
On 12/04/2025 03:43, Kaz Kylheku wrote:On 2025-04-12, bart <bc@freeuk.com> wrote:>>C type syntax is famous for being difficult and confusing; I think most
will agree about that. Even the creators said so.
If you had a function that takes an int, that returned a pointer to an
array, how would you pass in 42, and then get at the third element?
f(42) // gets the pointer to the array
(*f(42)) // designates the array
(*f(42))[2] // gets at the third element.
Ok, now declaring a function of int, which returns a pointer to an array
of 16 elements:
(*f(int))[16];
Notice any resemblance? The 42 argumet has changed to a type specifier
for the corresponding parameter type. The array reference turns into
the size. Minor!We need a type specifier to the elements:>
double (*f(int))[16];
Declaration follows use: it's not just a slogan, it's real!
I'm sorry, but it doesn't work! The thought processes are entirely
different between writing expressions, and declaring types. They differ
in many ways anyway:
* A typespec needs a base type; an expr doesn't
* A typespec needs to express the whole thing right up to the base type;
an expr can be partial
* A typespec can include 'const' and other attributes; an expr doesn't
* A typespec uses '*' for pointers, an expr can use '*' or '[]' to deref
* A typespec uses '[]' for arrays; an expression can choose to use '*'
to index
* A typespec uses (*) for function pointers, but an expression can
choose to omit both that * and the accompanying ().
C doesn't directly have the means to centralise a common type, you have
to do:
>
typedef int (*Arr)[10];
Arr A, B, C;
>
or:
typeof(int (*)[10]) , B, C
Here there is a clear (and clean!) disconnect between each variable, and
its type. A variable's type shouldn't be something it has to 'wear'
wrapped around it, so that it literally looks like an expression. That
would be a bizarre concept.
Les messages affichés proviennent d'usenet.