Translation of C function pointer.
Jonathan M Davis
jmdavisProg at gmx.com
Fri Sep 17 11:00:49 PDT 2010
On Friday, September 17, 2010 10:43:12 Ali Çehreli wrote:
> Jonathan M Davis wrote:
> > On Thursday 16 September 2010 23:50:16 Kagamin wrote:
> >> BCS Wrote:
> >>> The trick is that function pointers are best read from the inside out.
> >>
> >> All C declarations are read from inside out, postfixes take precedence,
> >> that's why you have to use braces to give pointer higher precedence.
> >> One of the earlier books by Stroustroup gives a nice monster of
> >> arrays, pointers and functions to master understanding of
> >> declarations.
> >
> > It's essentially the same principle that makes it so that the D
>
> declaration
>
> > int[4][3] a;
> >
> > is an array with 3 rows and 4 columns rather than 4 rows and 3
>
> columns like
>
> > you'd expect.
> >
> > - Jonathan M Davis
>
> I don't see it that D's declaration follows C's at least in array
> declarations:
>
> int[4] is an array of 4 ints; like Simen, let's call it U.
> Now U[3] is an array of 3 Us; i.e. 3 int[4]s
>
> I read that from left to right, not inside out.
No, no. You read it outwards from the variable name (which is essentially what
you're doing in C), so you read it from right to left. If you read it from left
to right it would be an integer of arrays, which makes no sense. It's just like
how int* is a pointer to an int, not an int to a pointer. Declarations are read
outwards from the variable name (which is usually right to left).
>
> The equivalent C declaration has the opposite type:
>
> int a[4][3];
>
> Now that is 4 arrays of 3 ints.
>
> D wins big time here! :)
Again, you're reading outwards from the variable name. That means that you read
this one left to right, though it gets a bit weird because the type is on both
sides of the variable name (though you get the same problem with function
pointers). Both
int[3][4] a;
and
int a[4][3];
are 4 arrays of 3 ints, and both read outwards from the variable name. And
because when you index them, the max indices would be a[3][2], it totally throws
people off to declare them the D way because the order is reversed from where it
was declared (since you're using the brackets on the opposite side of the
variable name now).
The fact that int[3][4] a; is 4 arrays of 3 ints is _exactly_ the same as how it
works with C declarations. You read outward from the variable name.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list