Confusion about static arrays

Kirk McDonald kirklin.mcdonald at gmail.com
Sat Jan 27 20:10:13 PST 2007


Rick Mann wrote:
> I had a little hiccup earlier when I tried to do this:
> 
> 	static EventTypeSpec[1] events =
> 	[
> 		{ EventClass.kEventClassWindow, kEventWindowBoundsChanged }
> 	];
> 	writefln("events: 0x%x", events);
> 
> (EventTypeSpec is a struct with two uint members).
> 
> This resulted in a bus error inside writefln(). Someone pointed out that I should pass events.ptr, which works. However, from the examples in (http://www.digitalmars.com/d/arrays.html). For example, it says that static arrays are analogous to C arrays. It also suggests this:
> 
> int* p;
> int[3] s;
> int[] a;
> 
> p = s;		// p points to the first element of the array s.
> p = a;		// p points to the first element of the array a.
> 
> In both cases, assigning from s or a results in something pointing to the first element of s and a. 
> 
> I guess I have two questions:
> 
> a) why would passing "events" to writefln() (with that specific format specifier) cause it all to crash

It seems that writefln doesn't know how to convert a static array to a 
hex number. It does, however, know how to write a pointer in hex, so 
using .ptr works.

> b) Is the spec web page incorrect (or is it reasonable to be mislead by its wording) when it seems to imply that one can simply pass the name of the array around, rather than .ptr?

The spec is incorrect. D was changed a few versions ago to disallow this 
implicit conversion from arrays to pointers.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org


More information about the Digitalmars-d-learn mailing list