Cannot cast void* to arrays..?

Ali Çehreli acehreli at yahoo.com
Fri Feb 24 11:56:18 PST 2012


On 02/24/2012 11:44 AM, simendsjo wrote:
> On Fri, 24 Feb 2012 20:42:20 +0100, Justin Whear
> <justin at economicmodeling.com> wrote:
>
>> On Fri, 24 Feb 2012 20:34:19 +0100, simendsjo wrote:
>>
>>> char[] a;
>>> auto b = cast(void*)a;
>>> auto c = cast(char[])b; // Error: e2ir: cannot cast b of type void*
>>> to
>>> type char[]
>>
>> Arrays have a length--you need to cast the pointer to a char*, then slice
>> it.
>
> Ah, of course, thanks.
> But what about static arrays?
> char[1] a;
> //a.length = 10; // constant a.length is not an lvalue
> auto b = cast(void*)a;
> auto c = cast(char[1])b; // Error: e2ir: cannot cast b of type void* to
> type char[1LU]

     char[1] a;
     auto c = a.ptr[0..a.length];

Ali


More information about the Digitalmars-d-learn mailing list