Why can you cast an array to a pointer?

Quirin Schroll qs.il.paperinik at gmail.com
Fri May 12 17:20:49 UTC 2023


On Thursday, 11 May 2023 at 20:44:36 UTC, Steven Schveighoffer 
wrote:
> Why is this still possible?
>
> ```d
> int[] arr = new int[50];
> auto ptr = cast(int *)arr;
> ```
>
> 1. casting is a blunt instrument, and can cause some really bad 
> problems on code eveolution.
> 2. Literally easier to do arr.ptr instead of cast(T*)arr
>
> Can we get rid of this? If you need the cast mechanism, and you 
> really mean it, you can do `cast(T*)arr.ptr`
>
> The biggest problem is that people using C API which take `char 
> *` instead of `string` or whatnot, use `cast` and come to 
> discord/etc with the inevitable problems this causes.

Yes, please. D is a language for which “most of its code is being 
written in the future” is probably true. It has a simple 
transition path.


More information about the Digitalmars-d mailing list