www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array of pointers

reply "Arjan Fetahu" <21arjan hotmail.com> writes:
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
parent reply "Namespace" <rswhite4 googlemail.com> writes:
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
 Arjan
node*[] nedePtr;
Jan 16 2014
parent reply "Arjan Fetahu" <21arjan hotmail.com> writes:
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:
 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
node*[] nedePtr;
Ok. Thank You!
Jan 16 2014
parent reply "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
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:
 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
 Arjan
node*[] nedePtr;
Ok. Thank You!
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.
Jan 16 2014
next sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
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:
 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:
 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
node*[] nedePtr;
Ok. Thank You!
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 16 2014
parent reply "Arjan Fetahu" <21arjan hotmail.com> writes:
 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; } ----
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.. }
Jan 18 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent reply "Arjan Fetahu" <21arjan hotmail.com> writes:
 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
Youre 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
parent "Gary Willoughby" <dev nomad.so> writes:
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.

 Arjan
Here's a handy introduction: http://nomad.so/2013/07/templates-in-d-explained/
Jan 20 2014
prev sibling next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
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
prev sibling next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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.
Jan 20 2014
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jan 20 2014
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
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; } ```
Jan 20 2014