arrays and foreach

Somedude lovelydear at mailmetrash.com
Tue Apr 17 00:42:45 PDT 2012


Le 17/04/2012 02:01, Ali Çehreli a écrit :
> On 04/16/2012 04:56 PM, darkstalker wrote:
>> i have this example program:
>>
>> ---
>> void main()
>> {
>> int[3] a;
>> foreach (p; a)
>> p = 42;
>> writeln(a);
>> }
>> ---
>>
>> after running it, i expect to get [42, 42, 42] but instead i get [0, 0,
>> 0] (i know that you can do a[] = 42, it's just a trivial example). So it
>> seems that you cannot write into the array because the elements are
>> being passed by value each iteration. It possible to have 'p' passed by
>> reference?
> 
> Yes:
> 
>     foreach (ref p; a)
> 
> Ali
> 
> 
Hi Ali,

Sorry for hijacking this thread, but since you're around, I hope you'll
see this message. As a D beginner, I'm browsing through your book.
I wanted to tell you that there is something essential missing in it:
how to compile. It's actually quite hard to find it in the official
website, and since the first chapters are targetted at complete
beginners, I thought it would be nice to add a few lines or pages about
compilation, linking, debugging, etc.
What do you think ?


More information about the Digitalmars-d-learn mailing list