Array declaration warning

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 2 18:58:58 PDT 2015


On 6/2/15 9:43 PM, Mike Parker wrote:
> On 6/3/2015 1:37 AM, Paul wrote:
>> On Tuesday, 2 June 2015 at 16:23:32 UTC, Adam D. Ruppe wrote:
>>> On Tuesday, 2 June 2015 at 16:17:23 UTC, Paul wrote:
>>>> CoOrd[NumPaths][] pathList;
>>>
>>> Try CoOrd[][NumPaths].
>>>
>>> The order is opposite in D style than in C style (I thnk the D style
>>> makes more sense, it just reads one direction, but it does index
>>> differently than you're used to in C)
>>
>> Thank you - I can see this will cause me no end of problems until I get
>> my head 'round it!
>>
>
> int[] -> A dynamic array of int
> int*[] -> A dynamic array of int*
> int[][] -> A a dynamic array of int[]
>
> int[3] -> a static array of int
> int*[3] -> a static array of int*
> int[][3] -> a static array of int[]

To further demonstrate the logic of the grammar:

An array looks like this:

T[]

Where T is some type.

So whenever you see this pattern, you know this is an array of that 
type, even if that type is more arrays!

int[] => T == int
int*[] => T == int*
int[][] => T == int[]

The same works for static arrays, except those have a builtin length.

It's actually quite simple and logical. What can confuse people is when 
using the array, you reverse the indices:

int[4][3] arr; // A static array of 3 elements, each element is a static 
array of 4 elements of int.

So the length of the indexes is reversed, the first position has length 
of 3, the second has length of 4.

-Steve


More information about the Digitalmars-d-learn mailing list