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:
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
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 anonthis is not C.
Jan 21 2015
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:Your right its c++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; anonthis is not C.
Jan 21 2015
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: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.On Wed, 21 Jan 2015 23:44:49 +0000 anon via Digitalmars-d-learn=20 <digitalmars-d-learn puremagic.com> wrote:=20 Your right its c++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 anonthis is not C.
Jan 21 2015
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: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. anonOn Wednesday, 21 January 2015 at 23:47:46 UTC, ketmar via Digitalmars-d-learn wrote: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.On Wed, 21 Jan 2015 23:44:49 +0000 anon via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Your right its c++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; anonthis is not C.
Jan 21 2015
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
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
On Thursday, 22 January 2015 at 00:16:23 UTC, bearophile wrote:anon:Hi Bearophile, It looks like what I need. Thanks, anonI 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
anon:It looks like what I need.But what's the purpose of the last two empty arrays? Bye, bearophile
Jan 21 2015
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; anonThe 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