how to initialize an array of typedef-ed type?

MLT none at anone.com
Fri May 1 07:07:48 PDT 2009


Jarrett Billingsley Wrote:

> On Fri, May 1, 2009 at 9:15 AM, MLT <none at anone.com> wrote:
> >
> > typedef long location ;
> > location a[] = cast(location[])[1,2,3,4] ;
> >
> > Seems to work.
> > I was afraid of the mess that would happen if I cast (int *) to (long *) in C...
> > Can I trust it? Is that the "right way" to do it?
> 
> Have you actually run it and seen what the results are?  If it works,
> well, use it :)
> 
> (It does work, and that is the right way to do it.)

Yes, it does work!

At first I thought that maybe on my machine int and long have the same length, or some such. But it works, for int, long, and short. And they do have different lengths.

I tried a bit more:
---
typedef long tlong ;

void main()
{
	int[] x = [1,2,3,4] ;
	
	tlong[] y = cast(tlong[])[1,2] ; // <- this works

	Stdout(y).newline ;

	y = cast(tlong[]) x ; // This doesn't
	
	Stdout(y).newline ;
}
---
prints out:
[1, 2]
[8589934593, 17179869187]

The the initialization works, but casting from int[] to long[] in general doesn't.


More information about the Digitalmars-d-learn mailing list