September 17, 2010
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
September 17, 2010
On Fri, 17 Sep 2010 10:12:34 +0200, Jonathan M Davis <jmdavisProg@gmx.com> 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.

I've always been confused by C in this regard. It seems to logical to me
that T[3] works the same whether T is U[4] or U.

-- 
Simen
September 17, 2010
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.

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! :)

Two programs to test:

#include <stdio.h>
int main()
{
    int a[4][3];
    printf("%u\n", sizeof(a[0]));

    return 0;
}

Outputs 12.

import std.stdio;
void main()
{
    int[4][3] a;
    writeln(typeof(a[0]).sizeof);
}

Outputs 16.

Ali
September 17, 2010
On Friday, September 17, 2010 05:00:55 Simen kjaeraas wrote:
> On Fri, 17 Sep 2010 10:12:34 +0200, Jonathan M Davis <jmdavisProg@gmx.com>
> 
> 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.
> 
> I've always been confused by C in this regard. It seems to logical to me that T[3] works the same whether T is U[4] or U.

You're going to have to elaborate on that. I'm not quite sure what you're talking about. And the syntax int[4][3] isn't legal C anyway. It just does what C would likely have done had it put the brackets with the type rather than the variable name, since D uses pretty much the same syntax for variable declarations and therefore pretty much the same rules.

- Jonathan M Davis
September 17, 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
September 17, 2010
Jonathan M Davis Wrote:

> On Friday, September 17, 2010 10:43:12 Ali Çehreli wrote:
> >
> > 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).

That isn't how you read it.

int* b;

int pointer b.

int[4][3] a;

int array of 4, array of 3 a.

This isn't exactly grammatically clear English. But it isn't English it is D.

int c[3][4];

c is array of 3 int arrays of 4 int. Definitely not left to right.
September 18, 2010
Jonathan M Davis <jmdavisProg@gmx.com> wrote:

>> I've always been confused by C in this regard. It seems to logical to me
>> that T[3] works the same whether T is U[4] or U.
>
> You're going to have to elaborate on that. I'm not quite sure what you're
> talking about. And the syntax int[4][3] isn't legal C anyway. It just does what
> C would likely have done had it put the brackets with the type rather than the
> variable name, since D uses pretty much the same syntax for variable
> declarations and therefore pretty much the same rules.

You know, after reading your post to Ali, and trying to come up with a
reason why I felt the way I did, I have found enlightenment.
Limited, yes, but I now see that C's order is also good. Now if only
there were a reasonable way to extricate the type from the variable
name...

-- 
Simen
September 18, 2010
On Friday, September 17, 2010 17:07:15 Simen kjaeraas wrote:
> Jonathan M Davis <jmdavisProg@gmx.com> wrote:
> >> I've always been confused by C in this regard. It seems to logical to me that T[3] works the same whether T is U[4] or U.
> > 
> > You're going to have to elaborate on that. I'm not quite sure what you're
> > talking about. And the syntax int[4][3] isn't legal C anyway. It just
> > does what
> > C would likely have done had it put the brackets with the type rather
> > than the
> > variable name, since D uses pretty much the same syntax for variable
> > declarations and therefore pretty much the same rules.
> 
> You know, after reading your post to Ali, and trying to come up with a
> reason why I felt the way I did, I have found enlightenment.
> Limited, yes, but I now see that C's order is also good. Now if only
> there were a reasonable way to extricate the type from the variable
> name...

Well, it's easy to get the type in D, but C is not so enlightened. That's what happens when you compare languages which have about a 25 year difference in age.

- Jonathan M Davis
1 2
Next ›   Last »