How to pass in reference a fixed array in parameter

bauss jacobbauss at gmail.com
Wed Jun 5 15:08:34 UTC 2024


On Wednesday, 5 June 2024 at 06:22:34 UTC, Eric P626 wrote:
>
> I tried the following signatures with the ref keyword and it 
> did not change anything:
>
> ~~~
> void print_maze ( ref s_cell maze )
> void print_maze ( ref s_cell [][] maze )
> ~~~
>
> From what I found, arrays passed in parameters are always 
> passed by reference. So the ref keyword seems pointless.
>

There is one useful functionality about the ref keyword when 
passing arrays, it is that you can change the original array 
reference to another array reference.

Ex.

```
void foo(ref int[] x)
{
     x = [1,2,3];
}

void bar(int[] y)
{
     y = [1,2,3];
}

void main()
{
     auto x = [0,0,0];
     auto y = [1,1,1];

     foo(x);
     bar(y);

     writeln(x);
     writeln(y);
}
```

The output of the program is:

```
[1, 2, 3]
[1, 1, 1]
```

Of course in your case this doesn't matter, but just wanted to 
point out that adding ref to array parameters actually pose a 
function.


More information about the Digitalmars-d-learn mailing list