www.digitalmars.com         C & C++   DMDScript  

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

reply "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
next sibling parent reply 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
parent reply "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.
Your right its c++
Jan 21 2015
parent reply 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 Your right its 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.
Jan 21 2015
parent reply "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.
Your right its 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.
Great answer. 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
parent "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
prev sibling next sibling parent reply "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 :-) 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
Jan 21 2015
parent reply "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 :-) 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
Hi Bearophile, It looks like what I need. Thanks, anon
Jan 21 2015
parent "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
prev sibling parent "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