Thread overview | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 01, 2002 Casting arrays | ||||
---|---|---|---|---|
| ||||
The D section on arrays mentions casting twice: 1) Casting char[] to wchar[] and vice versa, where the original value was a string constant (wchar[])"abc"; /* legal, creates a wchar string */ (ascii)"\u1234"; /* illegal, can't cast non-ascii wchars to ascii */ 2) Casting arrays to pointers char abc[]; char *p = (char*)abc; But can you can an array variable from one type to another? char abc[]; ubyte other[] = (ubyte[])abc; /* note: ubyte is 8 bits, just like char */ The doc doesn't say (that I've found). I think that you should be able to do this, PROVIDED THAT the underlying types have the same size. If we go with this rule, then we also can cast char[] to wchar[]. They don't have the same size, so the compiler automatically rewrites the char[] as the new wchar[] in a new array: char abc[] = "hi"; wchar wabc[] = (wchar[])abc; /* wabc now = (wchar[])"hi" */ * Casts between types of the same size would NOT create a new array. * Casts between char & wchar WOULD create a new array that has the same string in the new representation Now if we do this, I would also recommend that we not let numeric operations on char or wchar anymore; instead, you cast them to an integer: char abc[] = "asdf"; abc += 1; /* illegal, can't do numeric operations on char */ ((byte[])abc) += 1; /* legal, modifies the abc array */ /* abc now = "bteg" */ -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
May 01, 2002 Re: Casting arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | You should also be able to cast arrays of class references to arrays of related class references, with the proper typechecking on each element (if typechecking would have been done on a cast of a single reference). -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
May 04, 2002 Re: Casting arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3CD0174F.5E91B48B@deming-os.org... > The D section on arrays mentions casting twice: > 1) Casting char[] to wchar[] and vice versa, where the original > value was a string constant > (wchar[])"abc"; /* legal, creates a wchar string */ > (ascii)"\u1234"; /* illegal, can't cast non-ascii wchars to > ascii */ > 2) Casting arrays to pointers > char abc[]; > char *p = (char*)abc; > > But can you can an array variable from one type to another? > char abc[]; > ubyte other[] = (ubyte[])abc; /* note: ubyte is 8 bits, just like > char */ > > The doc doesn't say (that I've found). I think that you should be able to do this, PROVIDED THAT the underlying types have the same size. > > If we go with this rule, then we also can cast char[] to wchar[]. They > don't have the same size, so the compiler automatically rewrites the > char[] as the new wchar[] in a new array: > char abc[] = "hi"; > wchar wabc[] = (wchar[])abc; /* wabc now = (wchar[])"hi" */ > > * Casts between types of the same size would NOT create a new array. > * Casts between char & wchar WOULD create a new array that has the same > string in the new representation > > Now if we do this, I would also recommend that we not let numeric operations on char or wchar anymore; instead, you cast them to an integer: > > char abc[] = "asdf"; > abc += 1; /* illegal, can't do numeric operations on char */ > ((byte[])abc) += 1; /* legal, modifies the abc array */ > /* abc now = "bteg" */ Some good questions. I'll try to answer them with a more general statement. I'm uncomfortable with casting that results in data being allocated and copied - too much going on behind the scenes. I admit that's a philosophical point. The second is that, in the past, I got pretty frustrated with Pascal disallowing arithmetic operations on chars. All the casting necessary just wound up being an irritating eyesore. I think of chars more as a special type of integers than as actual characters. |
May 05, 2002 Re: Casting arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:ab1mag$s75$1@digitaldaemon.com... > <SNIP> > ...I think of chars more as a special type of integers > than as actual characters. > That's your C heritage speaking :) -- Stijn OddesE_XYZ@hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail |
May 05, 2002 Re: Casting arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to OddesE | "OddesE" <OddesE_XYZ@hotmail.com> wrote in message news:ab3p6p$2p7t$1@digitaldaemon.com... > That's your C heritage speaking :) "If I want to add 10 to 'A', it should be not the compiler who tells me that I can't do it!" I dont remember now who said it, but I second. =) |
May 05, 2002 Re: Casting arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | "Pavel Minayev" <evilone@omen.ru> wrote in message news:ab3sve$2sgd$1@digitaldaemon.com... > "OddesE" <OddesE_XYZ@hotmail.com> wrote in message news:ab3p6p$2p7t$1@digitaldaemon.com... > > > That's your C heritage speaking :) > > "If I want to add 10 to 'A', it should be not the compiler who tells me that > I can't do it!" > > I dont remember now who said it, but I second. =) > Ofcourse, but a cast isn't such a bad thing here is it? -- Stijn OddesE_XYZ@hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail |
May 05, 2002 Re: Casting arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to OddesE | "OddesE" <OddesE_XYZ@hotmail.com> wrote in message news:ab3p6p$2p7t$1@digitaldaemon.com... > "Walter" <walter@digitalmars.com> wrote in message > news:ab1mag$s75$1@digitaldaemon.com... > <SNIP> > > ...I think of chars more as a special type of integers > > than as actual characters. > That's your C heritage speaking :) Yes, it is. But remember I came to C from Pascal (I actually wrote a mini-Pascal compiler once), and never looked back. |
May 05, 2002 Re: Casting arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to OddesE | "OddesE" <OddesE_XYZ@hotmail.com> wrote in message news:ab3v7p$2ug8$1@digitaldaemon.com... > "Pavel Minayev" <evilone@omen.ru> wrote in message news:ab3sve$2sgd$1@digitaldaemon.com... > > "OddesE" <OddesE_XYZ@hotmail.com> wrote in message news:ab3p6p$2p7t$1@digitaldaemon.com... > > > That's your C heritage speaking :) > > "If I want to add 10 to 'A', it should be not the compiler who tells me > that > > I can't do it!" > > I dont remember now who said it, but I second. =) Needing casts for ordinary mundane operations means there's a bug in the language design. |
May 06, 2002 Re: Casting arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote: > ...in the past, I got pretty frustrated with Pascal > disallowing arithmetic operations on chars. All the casting necessary just > wound up being an irritating eyesore. I think of chars more as a special > type of integers than as actual characters. I think that if we look at D, most of the old needs for arithmetic manipulation of chars is now gone. Think of what chars were used for in C: * As explicit 8-bit integers (since short might or might not be 8 bit) - D now has 'byte' and 'ubyte' * As strings - now D handles them much better than C, now almost no need for tests for the null terminator * Comparing strings - not really needed if you have strcmp() or a D equivalent * As cases in switch()es - but no reason that still can't work in D I don't think that you will need to cast chars to bytes very often in D... -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
May 06, 2002 Re: Casting arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote: > "OddesE" <OddesE_XYZ@hotmail.com> wrote in message news:ab3v7p$2ug8$1@digitaldaemon.com... > > "Pavel Minayev" <evilone@omen.ru> wrote in message news:ab3sve$2sgd$1@digitaldaemon.com... > > > "OddesE" <OddesE_XYZ@hotmail.com> wrote in message news:ab3p6p$2p7t$1@digitaldaemon.com... > > > > That's your C heritage speaking :) > > > "If I want to add 10 to 'A', it should be not the compiler who tells me > > that > > > I can't do it!" > > > I dont remember now who said it, but I second. =) > > Needing casts for ordinary mundane operations means there's a bug in the language design. Careful, Walter - that logic is a very slippery slope :) long *ptr; ..... ptr = cast(long*)(cast(byte*)ptr +1 ); /* add one byte to the pointer */ Remember, adding one byte to a pointer is a mundane operation...but it requires *2* casts. -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
Copyright © 1999-2021 by the D Language Foundation