Jump to page: 1 2
Thread overview
Array of pointers
Jan 16, 2014
Arjan Fetahu
Jan 16, 2014
Namespace
Jan 16, 2014
Arjan Fetahu
Jan 16, 2014
Rene Zwanenburg
Jan 16, 2014
Namespace
Jan 18, 2014
Arjan Fetahu
Jan 18, 2014
bearophile
Jan 18, 2014
Arjan Fetahu
Jan 20, 2014
Gary Willoughby
Jan 16, 2014
Philippe Sigaud
Jan 20, 2014
Andrej Mitrovic
Jan 20, 2014
Ali Çehreli
Jan 20, 2014
Philippe Sigaud
January 16, 2014
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
January 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
> Arjan

node*[] nedePtr;
January 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:
>> 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!
January 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:
>> 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.
January 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:
>> 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;
}
----
January 16, 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).
January 18, 2014
>> 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..
}
January 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
January 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,
> 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
January 20, 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.
>
> Arjan

Here's a handy introduction: http://nomad.so/2013/07/templates-in-d-explained/
« First   ‹ Prev
1 2