basic question about adresses and values in structs

Gary Willoughby via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 1 11:13:48 PDT 2014


On Monday, 1 September 2014 at 18:08:48 UTC, nikki wrote:
> so I am still very new to structs and & and * adress and 
> pointer stuff, I have this basic code :
>
>     struct S {
>         int value = 0;
>     }
>
>     void func(S thing){
>         writeln(&thing); //BFC52B44
>         thing.value = 100;
>     }
>
>     S guy = {value:200};
>     writeln(&guy); //BFC52CCC
>     func(guy);
>     writeln(guy.value);// this prints 200, because the adress 
> was not the same
>
> I think I see whats going on but I don't know how to fix it?

void func(ref S thing){
	writeln(&thing);
	thing.value = 100;
}

The ref keyword passes the variable into the function by 
reference, so that it is not copied.


More information about the Digitalmars-d-learn mailing list