digitalmars.D.learn - Confusion about static arrays
- Rick Mann (18/18) Jan 27 2007 I had a little hiccup earlier when I tried to do this:
- Kirk McDonald (10/35) Jan 27 2007 It seems that writefln doesn't know how to convert a static array to a
- Rick Mann (3/6) Jan 28 2007 I should've been more clear. I wasn't actually trying to write the conte...
- torhu (24/50) Jan 27 2007 I don't know, you would have to look at the source code that comes with
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 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? Thanks!
Jan 27 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 crashIt 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
Jan 27 2007
Kirk McDonald Wrote: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.I should've been more clear. I wasn't actually trying to write the contents of the array, just the address of the first element in it. But your note about the spec helps me to clear things up. Thanks!
Jan 28 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 crashI don't know, you would have to look at the source code that comes with dmd to see why it crashes. "%x" is definitely not the correct format specifier for an array, though. The real problem is that writefln doesn't seem to know what to do with structs. Crashing is not a great solution, though. You can either do this: writefln("events: 0x%x, 0x%x", events[0].field1, events[0].field2); Or you can give the struct a toString() member. Then you can use "%s" as the format, or just nothing at all. writefln (which I seem to remember uses std.format.doFormat) will call your toString for each element. char[] toString() { return "{" ~ std.string.toString(field1) ~ ", " ~ std.string.toString(field2) "}"; } 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 name of the array doesn't get implicitly converted to a pointer to the data anymore. Other than that, you can pass arrays around as you please. Be aware that static arrays are also passed as length + pointer when used as variadic arguments, which messes up things when used with a C api.
Jan 27 2007