Double pointers

AndyC andy at squeakycode.net
Sat Nov 14 19:11:26 UTC 2026


On Saturday, 14 November 2026 at 18:41:06 UTC, AndyC wrote:
> Hi all.  I'm wondering if there is a way to avoid double 
> pointers in something like this:
>
> ```D
> module ptrptr;
>
> import std.stdio;
>
> struct Rec {
> 	string name;
> 	string pin;
> 	string id;
> }
>
> void main() {
> 	Rec[string] table;
> 	table["uart"] = Rec(name: "uart", pin: "K54");
> 	table["clk"] = Rec(name: "clk", pin: "A22");
>
>
> 	Rec*[string] pinIndex;
> 	foreach(n, ref r; table) {
> 		pinIndex[r.pin] = &r;
> 	}
>
> 	auto at = "K54" in pinIndex;
> 	if (at) {
> 		writeln(**at);
> 	}
> }
> ```
>
> I have a table of data that I'd like to find elements by name, 
> or by pin.  I'd like to be able to update the same element.  
> But when I search by pin, I get this **at double pointer.  It 
> works, but feels like there might be a better way.
>
> Thanks,
>
> -Andy

Oh, Ha!  I realized I should just ask Gemini.  It said I should 
use class instead of struct because:

1) its a reference, so both tables can just store a reference.
2) resizing table wont invalidate pointers in pinIndex.

Seems good to me.  (I didn't even think about #2)

-Andy


More information about the Digitalmars-d-learn mailing list