Liste des Groupes | Revenir à cl c |
On 2025-04-12, bart <bc@freeuk.com> wrote:
C type syntax is famous for being difficult and confusing; I think mostIf you had a function that takes an int, that returned a pointer to an
will agree about that. Even the creators said so.
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: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:
double (*f(int))[16];
Declaration follows use: it's not just a slogan, it's real!
If you don't like the declaration syntax, it's possibly becauseYes, making deref a prefix op was a bad idea. Postfix is much better (but the choice of '*' makes that problematical).
you don't like the expression syntax for working with pointer
dereferencing and arrays, which it mirrors. (You not liking
C expression syntax: billion to one odds, right?)
IDEA: maybe you would like the "dedclaration follows use", if it wasI'm lost, sorry. Things are getting more complicated, not simpler!
rendered over a different expression grammar in which pointer, arrays
and whatnot work the way you like. If didn't dislike the "use",
you might not dislike "declaration follows use".
Now, from this "envelope shape":
double (.......)[16];
we know that whatever ..... is, it is an array of 16 doubles.
This .... is called a "type hole" by functional programmers working
in Haskell and whatnot.
If we replace the hole with a name like abc:
double (abc)[16];
then what is being declared is that name. We get an object abc which is
such an array. The parentheses are then unnecessary and we can drop
them.
If we plug in *f(int) instead of abc, then *f(int) isn't what is
being declared: f is. But we know that when this f is called, and the
Les messages affichés proviennent d'usenet.