D (The Programming Language)/d2/Pointers, Pass-By-Reference and Static Arrays

=Lesson 7: Pointers, Pass-By-Reference and Static Arrays= In this chapter, you will learn about how to use pointers and static arrays in D. You will also learn how to pass by reference without using pointers. The lesson text will not explain the concepts behind pointers and arrays, and it will assume that you have that knowledge from C or C++. D also has dynamic arrays and array slices, both of which will be covered in a later lesson.

Pass By Reference
If you're a C++ programmer and you read the above thinking it's a step back from C++ references, then rejoice: while D supports pointers, it also supports better solutions for passing variables by reference to functions: There is also special support for out parameters: Using, n in initializeNumber is default initialized at the beginning of the function.

Reference and Dereference
The syntax in D is the same as C and C++. The operator  takes the reference of an object, and   dereferences.

Pointer Types
In D, a pointer to typeA is:  and the code for declaring one is:.

Static Arrays
Static arrays have fixed lengths. You declare them like this: You can also do this: Arrays are indexed starting with the number zero for the first element. The compiler will catch you if you attempt to access an element with an index greater than or equal to that array's length. You access elements of an array like this: You can also fill an array with a single value, so that all of that array's elements equal that value:

As Function Arguments
Look at this code:

The output is, not. Why? It's because static arrays are copied whenever they are passed as arguments to a function. The  in the   function is only a copy of. Rewrite it like this: