Accessing members through pointers to structs (also, CTFE associative arrays)

drug007 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Dec 13 13:08:31 PST 2016


On 13.12.2016 23:30, Ali wrote:
> Hi, Long time watcher and recently started playing with D a bit more.
> Ran in to a couple of snags that I'll combine in one post. It involves a
> data set that contains a list of strings. Each string represents a Room
> name. What I'm trying to do is pluck out the room names and also
> calculate the frequency each letter occurs in a name, per room.
>
> First problem is to do with pointers to structs. Here's the code:
>
> static immutable rooms = import("data.txt").split("\n").map!parse.array;
>
> static Tuple!(const(Room*), "room", int[char],
> "frequencies")[rooms.length] data;
> static this() {
>      foreach (i, room; rooms) {
>          data[i].room = &room;
>          // Also calculate frequencies, but that's not important yet.
>      }
> }
>
> void main() {
>      foreach (d; data) {
>          d.room.name.writeln; // <-- How do I access name here??
>      }
> }
>
> I've tried d.(*room).name but that didn't work. There's no arrow
I'm sleepy, sorry for quick and probable wrong answer - try (*d.room).name



More information about the Digitalmars-d-learn mailing list