| Thread overview | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 21, 2015 How can I convert the following C to D. | ||||
|---|---|---|---|---|
| ||||
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
| ||||
January 21, 2015 Re: How can I convert the following C to D. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to anon Attachments: | 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. | |||
January 21, 2015 Re: How can I convert the following C to D. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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++
| |||
January 21, 2015 Re: How can I convert the following C to D. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to anon Attachments: | 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. | |||
January 22, 2015 Re: How can I convert the following C to D. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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
| |||
January 22, 2015 Re: How can I convert the following C to D. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to anon | 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
| |||
January 22, 2015 Re: How can I convert the following C to D. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to anon | 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);
| |||
January 22, 2015 Re: How can I convert the following C to D. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to anon | 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
| |||
January 22, 2015 Re: How can I convert the following C to D. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply