How can I convert the following C to D.

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jan 21 16:16:22 PST 2015


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 :-)

Is your code initializing info[r+1]?

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


More information about the Digitalmars-d-learn mailing list