## digitalmars.D.learn - How can I convert the following C to D.

• anon (10/10) Jan 21 2015 I have the following C code, how can I do the same in D.
• ketmar via Digitalmars-d-learn (3/16) Jan 21 2015 this is not C.
• anon (3/20) Jan 21 2015 Your right its c++
• ketmar via Digitalmars-d-learn (5/27) Jan 21 2015 so the answer to your question is very easy: just type in any
• anon (10/39) Jan 21 2015 Great answer.
• bearophile (5/8) Jan 21 2015 That's probably a better and simpler translation. I have tried to
• bearophile (32/41) Jan 21 2015 I suggest you to ignore ketmar, he's not helping :-)
• anon (5/48) Jan 21 2015 Hi Bearophile,
• bearophile (4/5) Jan 21 2015 But what's the purpose of the last two empty arrays?
• anonymous (31/41) Jan 21 2015 The super-literal translation:
"anon" <anon spam.com> writes:
```I have the following C code, how can I do the same in D.

Info **info;
info = new Info*[hl + 2];

int r;
for(r = 0; r < hl; r++)
{
info[r] = new Info[vl + 2];
}
info[r] = NULL;

anon
```
Jan 21 2015
ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
```On Wed, 21 Jan 2015 23:44:49 +0000
anon via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

I have the following C code, how can I do the same in D.
=20
Info **info;
info =3D new Info*[hl + 2];
=20
int r;
for(r =3D 0; r < hl; r++)
{
info[r] =3D new Info[vl + 2];
}
info[r] =3D NULL;
=20
anon

this is not C.
```
Jan 21 2015
"anon" <anon spam.com> writes:
```On Wednesday, 21 January 2015 at 23:47:46 UTC, ketmar via
Digitalmars-d-learn wrote:
On Wed, 21 Jan 2015 23:44:49 +0000
anon via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

I have the following C code, how can I do the same in D.

Info **info;
info = new Info*[hl + 2];

int r;
for(r = 0; r < hl; r++)
{
info[r] = new Info[vl + 2];
}
info[r] = NULL;

anon

this is not C.

```
Jan 21 2015
ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
```On Wed, 21 Jan 2015 23:50:59 +0000
anon via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

On Wednesday, 21 January 2015 at 23:47:46 UTC, ketmar via=20
Digitalmars-d-learn wrote:
On Wed, 21 Jan 2015 23:44:49 +0000
anon via Digitalmars-d-learn=20
<digitalmars-d-learn puremagic.com> wrote:

I have the following C code, how can I do the same in D.
=20
Info **info;
info =3D new Info*[hl + 2];
=20
int r;
for(r =3D 0; r < hl; r++)
{
info[r] =3D new Info[vl + 2];
}
info[r] =3D NULL;
=20
anon

this is not C.

=20

so the answer to your question is very easy: just type in any
gibberish. as C cannot compile C++ code, the final result is to get the
code that cannot be compiled. any gibberish will do.
```
Jan 21 2015
"anon" <anon spam.com> writes:
```On Wednesday, 21 January 2015 at 23:59:34 UTC, ketmar via
Digitalmars-d-learn wrote:
On Wed, 21 Jan 2015 23:50:59 +0000
anon via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

On Wednesday, 21 January 2015 at 23:47:46 UTC, ketmar via
Digitalmars-d-learn wrote:
On Wed, 21 Jan 2015 23:44:49 +0000
anon via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

I have the following C code, how can I do the same in D.

Info **info;
info = new Info*[hl + 2];

int r;
for(r = 0; r < hl; r++)
{
info[r] = new Info[vl + 2];
}
info[r] = NULL;

anon

this is not C.

so the answer to your question is very easy: just type in any
gibberish. as C cannot compile C++ code, the final result is to
get the
code that cannot be compiled. any gibberish will do.

Anyway the code isn't mine I just wanted to know how to handle
what the author wrote.

I got it working with.

auto info = new Info[][](hl, vl);

and changing the logic so as not check for the NULL.

No need on being picky it was just a question.

anon
```
Jan 21 2015
"bearophile" <bearophileHUGS lycos.com> writes:
```anon:

I got it working with.

auto info = new Info[][](hl, vl);

and changing the logic so as not check for the NULL.

That's probably a better and simpler translation. I have tried to
translate your original code too much literally.

Bye,
bearophile
```
Jan 21 2015
"bearophile" <bearophileHUGS lycos.com> writes:
```anon:

I have the following C code, how can I do the same in D.

Info **info;
info = new Info*[hl + 2];

int r;
for(r = 0; r < hl; r++)
{
info[r] = new Info[vl + 2];
}
info[r] = NULL;

I suggest you to ignore ketmar, he's not helping :-)

This is roughly a D translation (untested):

void main()  safe {
import std.stdio;

enum uint hl = 5;
enum uint vl = 7;
static struct Info {}

auto info = new Info[][](hl + 2);

foreach (ref r; info[0 .. hl])
r = new Info[vl + 2];

writefln("[\n%(%s,\n%)\n]", info);
}

Output:

[
[Info(), Info(), Info(), Info(), Info(), Info(), Info(), Info(),
Info()],
[Info(), Info(), Info(), Info(), Info(), Info(), Info(), Info(),
Info()],
[Info(), Info(), Info(), Info(), Info(), Info(), Info(), Info(),
Info()],
[Info(), Info(), Info(), Info(), Info(), Info(), Info(), Info(),
Info()],
[Info(), Info(), Info(), Info(), Info(), Info(), Info(), Info(),
Info()],
[],
[]
]

Is this what you look for?

Bye,
bearophile
```
Jan 21 2015
"anon" <anon spam.com> writes:
```On Thursday, 22 January 2015 at 00:16:23 UTC, bearophile wrote:
anon:

I have the following C code, how can I do the same in D.

Info **info;
info = new Info*[hl + 2];

int r;
for(r = 0; r < hl; r++)
{
info[r] = new Info[vl + 2];
}
info[r] = NULL;

I suggest you to ignore ketmar, he's not helping :-)

This is roughly a D translation (untested):

void main()  safe {
import std.stdio;

enum uint hl = 5;
enum uint vl = 7;
static struct Info {}

auto info = new Info[][](hl + 2);

foreach (ref r; info[0 .. hl])
r = new Info[vl + 2];

writefln("[\n%(%s,\n%)\n]", info);
}

Output:

[
[Info(), Info(), Info(), Info(), Info(), Info(), Info(),
Info(), Info()],
[Info(), Info(), Info(), Info(), Info(), Info(), Info(),
Info(), Info()],
[Info(), Info(), Info(), Info(), Info(), Info(), Info(),
Info(), Info()],
[Info(), Info(), Info(), Info(), Info(), Info(), Info(),
Info(), Info()],
[Info(), Info(), Info(), Info(), Info(), Info(), Info(),
Info(), Info()],
[],
[]
]

Is this what you look for?

Bye,
bearophile

Hi Bearophile,

It looks like what I need.

Thanks,
anon
```
Jan 21 2015
"bearophile" <bearophileHUGS lycos.com> writes:
```anon:

It looks like what I need.

But what's the purpose of the last two empty arrays?

Bye,
bearophile
```
Jan 21 2015
"anonymous" <anonymous example.com> writes:
```On Wednesday, 21 January 2015 at 23:44:52 UTC, anon wrote:
I have the following C code, how can I do the same in D.

Info **info;
info = new Info*[hl + 2];

int r;
for(r = 0; r < hl; r++)
{
info[r] = new Info[vl + 2];
}
info[r] = NULL;

anon

The super-literal translation:
Info** info;
info = new Info*[hl + 2].ptr;
int r;
for(r = 0; r < hl; r++)
{
info[r] = new Info[vl + 2].ptr;
}
info[r] = null;

A little more D-ish:
Info[][] info = new Info[][hl + 2];
foreach(r; 0 .. hl)
{
info[r] = new Info[vl + 2];
}
assert(info[hl].length == 0); /* Relying on default
initialization. */

D to the ultimate max:
import std.algorithm;
import std.range;
auto info = iota(hl)
.map!(i => new Info[vl + 2])
.chain(only(Info[].init, Info[].init))
.array;
static assert(is(typeof(info) == Info[][]));
assert(info.length == hl + 2);
assert(info[hl].empty);

And if those +2 are for sentinel values, D doesn't need them
because D arrays know their length. And it all comes down to:
auto info = new Info[][](hl, vl);
```
Jan 21 2015