foreach reference to struct variable

Denton Cockburn diboss at hotmail.com
Thu Feb 7 15:11:46 PST 2008


Prashant V Wrote:

> I wanted to use D for a simple program just to get a feel for how to use it, and while overall it was pretty easy, I found an annoying problem that I could not find an answer to online.
> 
> I want to be able to iterate through an array, and I want the current element to be the actual struct in the array itself. At the moment, it looks like the foreach is making a copy of the struct in the array so any changes are not made to the original in the array.
> 
> Is there any way to tell D to specifically give me the referenced variable rather than a copy? I had to resort to using for with an index, and I don't really think that is an ideal solution.
> 
> I want to know if there is a D syntax for the & reference modifier in C++.

use ref while indexing:

foreach (ref s; structs)
   // do something



More information about the Digitalmars-d mailing list