On 02/25/2012 07:15 PM, Timon Gehr wrote:
On 02/25/2012 07:03 PM, Chopin wrote:
Hello!

import std.stdio;

struct nagger
{
string name;
int age;
double weight;
string msg;
}

void main()
{
auto lal = new nagger();
lal.name = "AHAHAHAHHA";
lal.age = 23;
lal.weight = 108.5;
lal.msg = "fgfdgfdgfdgfdgfdgfdg";
writeln(cast(ubyte[])(lal));
}

Gives error: Error: e2ir: cannot cast lal of type nagger* to type ubyte[]
I have bad knowledge about this and don't understand why I cant do
this :(
What I really want is an array of naggers and save them binary to a
file :)
This was like my step 1, and it failed miserably...

Thanks for help.


Structs are not castable to arrays directly. You can achieve what you
want like this (untested, but should work).

(cast(void*)&lal)[0..nagger.sizeof];


You also have to change the first line of main to:

auto lol = nagger();

This will allocate the struct on the stack.

If you want to keep auto lal = new nagger(); for some reason, then do the cast like this:

(cast(void*)lal)[0..nagger.sizeof]

Reply via email to