www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - difference between void[] char[] and byte[]

reply nix <nix_member pathlink.com> writes:
Hello,   

i try to understand the difference between   
void[],char[] and byte[]   
is there any documentation about that?   


Ok, i think void[] has no typ.   
The read function in std.file use void[]   

import std.stdio;  
int main(){  

void[] buf = "house";  
char[] str = "windo";  
byte[] byt = cast(byte[])"floor";  

writefln("buf = %s",cast(char[])buf);  
writefln("void Lšnge = %d",buf.length);  
writefln("void size = %d\n",buf.sizeof);  
//writefln("Typ von %s",typeid(typeof(buf)));  // this didn't work for  
void[]  

writefln("str = %s",cast(char[])str);  
writefln("str Lšnge = %d",str.length);  
writefln("str size = %d",str.sizeof);  
writefln("Typ von %s\n",typeid(typeof(str)));  

writefln("byt = %s",cast(char[])byt);  
writefln("byt Lšnge = %d",byt.length);  
writefln("byt size = %d",byt.sizeof);  
writefln("Typ von %s",typeid(typeof(byt)));  

return 0;  
}  

Output:  

buf = house  
void Lšnge = 5  
void size = 8  

str = windo  
str Lšnge = 5  
str size = 8  
Typ von char[]  

byt = floor  
byt Lšnge = 5  
byt size = 8  
Typ von TypeInfo_Ag    ???   
Mar 16 2005
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
nix wrote:

 i try to understand the difference between   
 void[],char[] and byte[]   
 is there any documentation about that?   

void[] has a (void*) .ptr and holds "anything", char[] has a (char*) .ptr and holds UTF-8 code units, byte[] has a (byte*) .ptr and holds signed bytes... They all three have their .length given in bytes.
 writefln("buf = %s",cast(char[])buf);  
 writefln("void Lšnge = %d",buf.length);  
 writefln("void size = %d\n",buf.sizeof);  
 //writefln("Typ von %s",typeid(typeof(buf)));  // this didn't work for  
 void[]  

This a bug in Phobos, std/typeinfo/ti_Av.d is missing. :-(
 writefln("byt = %s",cast(char[])byt);  
 writefln("byt Lšnge = %d",byt.length);  
 writefln("byt size = %d",byt.sizeof);  
 writefln("Typ von %s",typeid(typeof(byt)));  

 Typ von TypeInfo_Ag    ???   

This is *another* Phobos bug, ti_Ag.d has no toString()... --anders
Mar 16 2005