Cannot cast void* to arrays..?
simendsjo
simendsjo at gmail.com
Fri Feb 24 12:12:12 PST 2012
On Fri, 24 Feb 2012 20:56:18 +0100, Ali Çehreli <acehreli at yahoo.com> wrote:
> 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
I don't get it. This gives me a dynamic array, not a static:
char[1] a;
auto b = cast(void*)a;
auto c = (cast(char*)b)[0..1];
c.length = 10; // auch!
More information about the Digitalmars-d-learn
mailing list