www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Cannot cast void* to arrays..?

reply simendsjo <simendsjo gmail.com> writes:
     char[] a;
     auto b = cast(void*)a;
     auto c = cast(char[])b; // Error: e2ir: cannot cast b of type void* to  
type char[]
Feb 24 2012
next sibling parent reply Justin Whear <justin economicmodeling.com> writes:
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.
Feb 24 2012
parent reply simendsjo <simendsjo gmail.com> writes:
On Fri, 24 Feb 2012 20:42:20 +0100, Justin Whear  
<justin 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]
Feb 24 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/24/2012 11:44 AM, simendsjo wrote:
 On Fri, 24 Feb 2012 20:42:20 +0100, Justin Whear
 <justin 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
Feb 24 2012
next sibling parent reply simendsjo <simendsjo gmail.com> writes:
On Fri, 24 Feb 2012 20:56:18 +0100, Ali =C3=87ehreli <acehreli yahoo.com=
 wrote:
 On 02/24/2012 11:44 AM, simendsjo wrote:
 On Fri, 24 Feb 2012 20:42:20 +0100, Justin Whear
 <justin economicmodeling.com> wrote:

 On Fri, 24 Feb 2012 20:34:19 +0100, simendsjo wrote:

 char[] a;
 auto b =3D cast(void*)a;
 auto c =3D cast(char[])b; // Error: e2ir: cannot cast b of type voi=
d*
 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 =3D 10; // constant a.length is not an lvalue auto b =3D cast(void*)a; auto c =3D cast(char[1])b; // Error: e2ir: cannot cast b of type void=
* to
 type char[1LU]
char[1] a; auto c =3D 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 =3D cast(void*)a; auto c =3D (cast(char*)b)[0..1]; c.length =3D 10; // auch!
Feb 24 2012
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 2/24/12, simendsjo <simendsjo gmail.com> wrote:
 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!
You can do: char[1] c = (cast(char*)b)[0..1];
Feb 24 2012
parent simendsjo <simendsjo gmail.com> writes:
On Fri, 24 Feb 2012 21:36:21 +0100, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 On 2/24/12, simendsjo <simendsjo gmail.com> wrote:
 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!
You can do: char[1] c = (cast(char*)b)[0..1];
Thanks! I had to do an explicit cast(char[1]) (or actually char[1][1] in my case.)
Feb 24 2012
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Feb 24, 2012 at 11:56:18AM -0800, Ali Çehreli wrote:
[...]
     char[1] a;
     auto c = a.ptr[0..a.length];
[...] Hey, that's an awesome way to implement copy-on-write static arrays! I'll have to use that sometime. :) T -- Sometimes the best solution to morale problems is just to fire all of the unhappy people. -- despair.com
Feb 24 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Feb 24, 2012 at 08:34:19PM +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[]
D arrays are not the same as C arrays. D arrays also include length in addition to the pointer, so you can't just cast a void* to an array. T -- Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Feb 24 2012
prev sibling next sibling parent Mantis <mail.mantis.88 gmail.com> writes:
24.02.2012 21:34, simendsjo пишет:
     char[] a;
     auto b = cast(void*)a;
     auto c = cast(char[])b; // Error: e2ir: cannot cast b of type 
 void* to type char[]
Generally, you should not cast a struct to pointer and vise-versa. Besides, size of array structure is larger than size of pointer, and that triggers error in your case.
Feb 24 2012
prev sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
 24.02.2012 21:34, simendsjo пишет:
    char[] a;
    auto b = cast(void*)a;
    auto c = cast(char[])b; // Error: e2ir: cannot cast b of type
void* to type char[]
[...] Just out of curiosity, what are you trying to accomplish with this cast? In almost all normal D code, there's no need for any casting at all. T -- The right half of the brain controls the left half of the body. This means that only left-handed people are in their right mind. -- Manoj Srivastava
Feb 24 2012
parent simendsjo <simendsjo gmail.com> writes:
On Fri, 24 Feb 2012 20:56:22 +0100, H. S. Teoh <hsteoh quickfur.ath.cx> =
 =

wrote:

 24.02.2012 21:34, simendsjo =D0=BF=D0=B8=D1=88=D0=B5=D1=82:
    char[] a;
    auto b =3D cast(void*)a;
    auto c =3D cast(char[])b; // Error: e2ir: cannot cast b of type
void* to type char[]
[...] Just out of curiosity, what are you trying to accomplish with this cas=
t?
 In almost all normal D code, there's no need for any casting at all.


 T
Interacting with a C callback taking a void*. In my callback, I want to = = get the same type back. See my previous question: = http://forum.dlang.org/post/op.v963zyg0x8p62v simendsjo-desktop (althoug= h = I didn't include the parameters in that example)
Feb 24 2012