digitalmars.D.learn - Array of pointers
- Arjan Fetahu (8/8) Jan 16 2014 Hi. I started my first program in D (I have a little experience
- Namespace (2/10) Jan 16 2014 node*[] nedePtr;
- Arjan Fetahu (2/14) Jan 16 2014 Ok. Thank You!
- Rene Zwanenburg (8/23) Jan 16 2014 Keep in mind that, unlike in c++, D classes are reference types:
- Namespace (21/46) Jan 16 2014 You mean:
- Arjan Fetahu (8/37) Jan 18 2014 I wanted to heap allocate Nodes which connect to each other via
- bearophile (7/14) Jan 18 2014 Nodes are reference types in D, so probably you don't need to use
- Arjan Fetahu (6/12) Jan 18 2014 Youre right, it compiles now, and the object generated is of the
- Gary Willoughby (3/7) Jan 20 2014 Here's a handy introduction:
- Philippe Sigaud (8/15) Jan 16 2014 That's not true. Indeed:
- Andrej Mitrovic (15/16) Jan 20 2014 Actually it's not, let's not confuse people with the terminology here.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (6/17) Jan 20 2014 I don't remember whether this thread explicitly excludes fixed-length
- Philippe Sigaud (11/16) Jan 20 2014 You're right, my bad.
Hi. I started my first program in D (I have a little experience in c). I wanted to create an array of pointers for creating a node with multiple connections. In C you can make one directly (node *nedePtr[]). What is the equivalent for the D's syntax?? Regards Arjan
Jan 16 2014
On Thursday, 16 January 2014 at 08:55:43 UTC, Arjan Fetahu wrote:Hi. I started my first program in D (I have a little experience in c). I wanted to create an array of pointers for creating a node with multiple connections. In C you can make one directly (node *nedePtr[]). What is the equivalent for the D's syntax?? Regards Arjannode*[] nedePtr;
Jan 16 2014
On Thursday, 16 January 2014 at 09:00:18 UTC, Namespace wrote:On Thursday, 16 January 2014 at 08:55:43 UTC, Arjan Fetahu wrote:Ok. Thank You!Hi. I started my first program in D (I have a little experience in c). I wanted to create an array of pointers for creating a node with multiple connections. In C you can make one directly (node *nedePtr[]). What is the equivalent for the D's syntax?? Regards Arjannode*[] nedePtr;
Jan 16 2014
On Thursday, 16 January 2014 at 09:03:00 UTC, Arjan Fetahu wrote:On Thursday, 16 January 2014 at 09:00:18 UTC, Namespace wrote:Keep in mind that, unlike in c++, D classes are reference types: class Node { Node[] nodes; // This is valid } Structs are value types though, so using a struct in the above example is illegal.On Thursday, 16 January 2014 at 08:55:43 UTC, Arjan Fetahu wrote:Ok. Thank You!Hi. I started my first program in D (I have a little experience in c). I wanted to create an array of pointers for creating a node with multiple connections. In C you can make one directly (node *nedePtr[]). What is the equivalent for the D's syntax?? Regards Arjannode*[] nedePtr;
Jan 16 2014
On Thursday, 16 January 2014 at 09:47:21 UTC, Rene Zwanenburg wrote:On Thursday, 16 January 2014 at 09:03:00 UTC, Arjan Fetahu wrote:You mean: ---- struct Node { Node[] nodes; } ---- or ---- struct Node { Node*[] nodes; } ---- ? Works both. What's not working is this: ---- struct Node { Node node; } ----On Thursday, 16 January 2014 at 09:00:18 UTC, Namespace wrote:Keep in mind that, unlike in c++, D classes are reference types: class Node { Node[] nodes; // This is valid } Structs are value types though, so using a struct in the above example is illegal.On Thursday, 16 January 2014 at 08:55:43 UTC, Arjan Fetahu wrote:Ok. Thank You!Hi. I started my first program in D (I have a little experience in c). I wanted to create an array of pointers for creating a node with multiple connections. In C you can make one directly (node *nedePtr[]). What is the equivalent for the D's syntax?? Regards Arjannode*[] nedePtr;
Jan 16 2014
I wanted to heap allocate Nodes which connect to each other via pointers. Since each Node connects to multiple others i came up with this solution. class Node { auto content; Node*[] nodes; //..constructor.. }Keep in mind that, unlike in c++, D classes are reference types: class Node { Node[] nodes; // This is valid } Structs are value types though, so using a struct in the above example is illegal.You mean: ---- struct Node { Node[] nodes; } ---- or ---- struct Node { Node*[] nodes; } ---- ? Works both. What's not working is this: ---- struct Node { Node node; } ----
Jan 18 2014
Arjan Fetahu:Since each Node connects to multiple others i came up with this solution. class Node { auto content; Node*[] nodes; //..constructor.. }Nodes are reference types in D, so probably you don't need to use a * for Node. Alternatively use a struct handled by pointer. "auto content;" can't compile, you need a type, or you have to template Node on T and use it for content. Bye, bearophile
Jan 18 2014
Nodes are reference types in D, so probably you don't need to use a * for Node. Alternatively use a struct handled by pointer. "auto content;" can't compile, you need a type, or you have to template Node on T and use it for content. Bye, bearophileYoure right, it compiles now, and the object generated is of the same size. I'm still confusing with C. I have some experience with C experience, so I still have to learn tamplates. Thaks for the help. Arjan
Jan 18 2014
On Saturday, 18 January 2014 at 14:57:39 UTC, Arjan Fetahu wrote:I have some experience with C experience, so I still have to learn tamplates. Thaks for the help. ArjanHere's a handy introduction: http://nomad.so/2013/07/templates-in-d-explained/
Jan 20 2014
On Thu, Jan 16, 2014 at 10:47 AM, Rene Zwanenburg <renezwanenburg gmail.com> wrote:Keep in mind that, unlike in c++, D classes are reference types: class Node { Node[] nodes; // This is valid } Structs are value types though, so using a struct in the above example is illegal.That's not true. Indeed: `struct Node { T value; Node[] children;}` is my standard way of having trees in D. The thing is, an array is a reference type and so the compiler is able to determine `Node` size (T.sizeof + a pointer size, or maybe 2, I don't remember which).
Jan 16 2014
On 1/16/14, Philippe Sigaud <philippe.sigaud gmail.com> wrote:The thing is, an array is a reference typeActually 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.
Jan 20 2014
On 01/20/2014 01:58 AM, Andrej Mitrovic wrote:On 1/16/14, Philippe Sigaud <philippe.sigaud gmail.com> wrote: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.) AliThe thing is, an array is a reference typeActually 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;
Jan 20 2014
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:You're right, my bad. TL;DR: you can do ``` struct Node { T data; Node[] children; } ```The thing is, an array is a reference typeActually 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.:
Jan 20 2014