www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why can you cast an array to a pointer?

reply Steven Schveighoffer <schveiguy gmail.com> writes:
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.

-Steve
May 11 2023
parent Quirin Schroll <qs.il.paperinik gmail.com> writes:
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.
May 12 2023