January 20, 2014 Re: Array of pointers | ||||
---|---|---|---|---|
| ||||
On 1/16/14, Philippe Sigaud <philippe.sigaud@gmail.com> wrote:
> The thing is, an array is a reference type
Actually it's not, let's not confuse people with the terminology here. To recap for people new to arrays: an array in D is really just a struct, e.g.:
struct Array
{
int* data;
size_t length;
}
Array myArray;
Once you know this it's easy to understand why passing myArray by value to a function allows you to change the contents of "data", but changing what "data" points to or changing the length of the array will not be reflected at the call site, *unless* you've passed myArray to a function by reference.
|
January 20, 2014 Re: Array of pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On 01/20/2014 01:58 AM, Andrej Mitrovic wrote: > On 1/16/14, Philippe Sigaud <philippe.sigaud@gmail.com> wrote: >> The thing is, an array is a reference type > > Actually it's not, let's not confuse people with the terminology here. > To recap for people new to arrays: an array in D is really just a > struct, e.g.: > > struct Array > { > int* data; > size_t length; > } > > Array myArray; I don't remember whether this thread explicitly excludes fixed-length arrays but to add to common confusion(s), the above is true only for slices (aka dynamic arrays). :) (And, although it shouldn't matter, the actual implementation defines the length member before the data member.) Ali |
January 20, 2014 Re: Array of pointers | ||||
---|---|---|---|---|
| ||||
On Mon, Jan 20, 2014 at 10:58 AM, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> On 1/16/14, Philippe Sigaud <philippe.sigaud@gmail.com> wrote:
>> The thing is, an array is a reference type
>
> Actually it's not, let's not confuse people with the terminology here. To recap for people new to arrays: an array in D is really just a struct, e.g.:
You're right, my bad.
TL;DR: you can do
```
struct Node
{
T data;
Node[] children;
}
```
|
Copyright © 1999-2021 by the D Language Foundation