Thread overview | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 02, 2015 Array declaration warning | ||||
---|---|---|---|---|
| ||||
This array of structures... CoOrd pathList[NumPaths][]; generates a warning when compiled with DMD 32 bit -w flag, but not 64 bit. If I correct the declaration to 'D style' CoOrd[NumPaths][] pathList; It compiles without warning but I cannot append items to the array like this... pathList[n] ~= CoOrd(cX, cY); What am I doing wrong? TIA Paul |
June 02, 2015 Re: Array declaration warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On Tuesday, 2 June 2015 at 16:17:23 UTC, Paul wrote: > This array of structures... > > CoOrd pathList[NumPaths][]; > > generates a warning when compiled with DMD 32 bit -w flag, but not 64 bit. This gives me a warning using rdmd on x64 XUbuntu. > If I correct the declaration to 'D style' > > CoOrd[NumPaths][] pathList; > > It compiles without warning but I cannot append items to the array like this... > > pathList[n] ~= CoOrd(cX, cY); > > What am I doing wrong? `CoOrd[NumPaths]` is a fixed-length array type containing `NumPaths` elements. You can't append to it. `CoOrd[NumPaths][]` is a dynamic array of such arrays, but `pathList[n]` gets one of the fixed-length arrays from the dynamic array. Perhaps you mean `CoOrd[][NumPaths]`? |
June 02, 2015 Re: Array declaration warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | 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)
|
June 02, 2015 Re: Array declaration warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex Parrill | On Tuesday, 2 June 2015 at 16:22:58 UTC, Alex Parrill wrote:
> On Tuesday, 2 June 2015 at 16:17:23 UTC, Paul wrote:
>> This array of structures...
>>
>> CoOrd pathList[NumPaths][];
>>
>> generates a warning when compiled with DMD 32 bit -w flag, but not 64 bit.
>
> This gives me a warning using rdmd on x64 XUbuntu.
>
>> If I correct the declaration to 'D style'
>>
>> CoOrd[NumPaths][] pathList;
>>
>> It compiles without warning but I cannot append items to the array like this...
>>
>> pathList[n] ~= CoOrd(cX, cY);
>>
>> What am I doing wrong?
>
> `CoOrd[NumPaths]` is a fixed-length array type containing `NumPaths` elements. You can't append to it. `CoOrd[NumPaths][]` is a dynamic array of such arrays, but `pathList[n]` gets one of the fixed-length arrays from the dynamic array.
>
> Perhaps you mean `CoOrd[][NumPaths]`?
Thank you, i guess that will work although it doesn't fit with my way of thinking about the problem, ie there are a fixed number of paths of variable length, eg...
pathList[0] (43, 60), (44, 60) ... <- append items
pathList[1] (55, 801), (56, 802), (56, 803) .... <- append items
pathList[2] etc
In case it's of interest I was compiling on Mint 17 64bit and Mint 17.1 32bit, both with latest DMD.
|
June 02, 2015 Re: Array declaration warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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!
Paul
|
June 03, 2015 Re: Array declaration warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | 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[]
|
June 03, 2015 Re: Array declaration warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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
|
June 03, 2015 Re: Array declaration warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | I used to not even notice that if D declare an array in C-style, and then indexing it will also in C-style :) Actually, I have a small question. And what is the advantage of indexing arrays D-style in front of a C-style arrays? Because C-style indexing is much more familiar: first, we point line, and then the columns. import std.algorithm; void main() { int cArr[2][4]; assert(equal(cArr[], [[0, 0, 0, 0], [0, 0, 0, 0]])); int[2][4] dArr; assert(equal(dArr[], [[0, 0], [0, 0], [0, 0], [0, 0]])); } |
June 03, 2015 Re: Array declaration warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On Wednesday, 3 June 2015 at 02:41:01 UTC, Dennis Ritchie wrote:
> Actually, I have a small question. And what is the advantage of indexing arrays D-style in front of a C-style arrays? Because C-style indexing is much more familiar: first, we point line, and then the columns.
That's what I meant by 'getting my head around it' - the concept
is simple enough, it's the years of habit that will be difficult
to break!
Paul
|
June 03, 2015 Re: Array declaration warning | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On Wednesday, 3 June 2015 at 11:42:35 UTC, Paul wrote:
>the concept is simple enough...
Well I thought it was; everything works as expected but I need to access values in the c style, i.e g the definitions:
struct CoOrd
{
int x, y;
}
CoOrd[][NumPaths]pathList;
|
Copyright © 1999-2021 by the D Language Foundation