Liste des Groupes | Revenir à cl c |
On 2025-04-12, bart <bc@freeuk.com> wrote:Those aren't direct. The only direct means C has is this form:On 12/04/2025 03:43, Kaz Kylheku wrote:The main difference is that the the expressions are the payload thatOn 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:
does the work, whereas declarations are just mental overead.
The thought process is that the C programmer has a data structure
layout in their mind and can write expressions to navigate through
it, from the top-level reference down to its leaves.
But, oh crap, the C programmer has to declare the things they are using
in that expression.
C brings a nice simplification here. If you can write the expression to
access the thing you are imagining, the declaration can be derived
from that shape.
* A typespec needs a base type; an expr doesn't"Declaration follow use" is not perfect or absolute, but it helps
* 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 ().
exactly with the confusing cases with mlutiple levels of pointers,
arrays and functions.
C doesn't directly have the means to centralise a common type, you haveWhy would you showing two ways how C has the direct means to centralize
to do:
>
typedef int (*Arr)[10];
Arr A, B, C;
>
or:
typeof(int (*)[10]) , B, C
a common array type, to illustrate the claim that it doesn't?
The simplest function pointer type in C is this:Here there is a clear (and clean!) disconnect between each variable, andIf you had thought of it first, you'd think it's brilliant.
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.
Dennis Ritchie really showed us a nice way to deal with messes
of pointers, arrays and function pointers.
Les messages affichés proviennent d'usenet.