How can I convert the following C to D.

2015-01-21 Thread anon via Digitalmars-d-learn

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


Re: How can I convert the following C to D.

2015-01-21 Thread anon via Digitalmars-d-learn
On Wednesday, 21 January 2015 at 23:59:34 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Wed, 21 Jan 2015 23:50:59 +
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 +
 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


Re: How can I convert the following C to D.

2015-01-21 Thread ketmar via Digitalmars-d-learn
On Wed, 21 Jan 2015 23:50:59 +
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 +
  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.


signature.asc
Description: PGP signature


Re: How can I convert the following C to D.

2015-01-21 Thread bearophile via Digitalmars-d-learn

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


Re: How can I convert the following C to D.

2015-01-21 Thread bearophile via Digitalmars-d-learn

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


Re: How can I convert the following C to D.

2015-01-21 Thread anonymous via Digitalmars-d-learn

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


Re: How can I convert the following C to D.

2015-01-21 Thread anon via Digitalmars-d-learn

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


Re: How can I convert the following C to D.

2015-01-21 Thread ketmar via Digitalmars-d-learn
On Wed, 21 Jan 2015 23:44:49 +
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.


signature.asc
Description: PGP signature


Re: How can I convert the following C to D.

2015-01-21 Thread anon via Digitalmars-d-learn
On Wednesday, 21 January 2015 at 23:47:46 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Wed, 21 Jan 2015 23:44:49 +
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++


Re: How can I convert the following C to D.

2015-01-21 Thread bearophile via Digitalmars-d-learn

anon:


It looks like what I need.


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

Bye,
bearophile