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

 Topics: Language and Phobos - DGui (example placeholder - can be any library) Previous Lessons: 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - T Next Subpages: What are Pointers? - What are 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.

## Introductory Code

### Using Pointers

```import std.stdio;

void add_numbers(int* a, int* b)
{
*a = *a + *b;
}

void main()
{
int a = 50;
int* b = &a;
*b = 30;
writeln(a);  // 30

// int* error = &50; Error: constant 50 is not an lvalue

int c = 2;
int d = 2;
writeln(c);  // 4
}
```

### 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:

```import std.stdio;

void add_numbers(ref int a, int b)
{
a += b;
}

void main()
{
int c = 2;
int d = 2;
writeln(c); // 4

// add_numbers(4, 2); Error: integer literals are not lvalues and cannot be passed by reference
}
```

There is also special support for out parameters:

```import std.stdio;

void initializeNumber(out int n)
{
n = 42;
}

void main()
{
int a;
initializeNumber(a);
writeln(a); // 42
}
```

Using `out`, n in initializeNumber is default initialized at the beginning of the function.

### Using Static Arrays

```import std.stdio;

void main()
{
int a;
a = 1;
writeln(a);  // [1, 0, 0, 0, 0]

// a = 3;  Error: Array index 10 is out of bounds

int* ptr_a = &a;
ptr_a = 5;
ptr_a = 3;
writeln(a); // [5, 0, 0, 0, 3]

// ptr_a = 3;  // Bad: This memory is not owned by 'a' as it is out of bounds
// Compiler would allow it, but the result is undefined!

int b = 3;
writeln(b);  // [3, 3, 3]
b[] = 2;
writeln(b);  // [2, 2, 2]
}
```

## Concepts

### 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:

```typeA*
```

and the code for declaring one is:

```typeA* identifier;
```

.

### Static Arrays

Static arrays have fixed lengths. You declare them like this:

```byte array_of_ten_bytes;
```

You can also do this:

```byte array_of_two_bytes = [39, 0x3A];
```

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:

```my_array[index]
```

You can also fill an array with a single value, so that all of that array's elements equal that value:

```int a = 2;
writeln(a);  // [2, 2, 2, 2, 2, 2, 2]
a[] = 7;
writeln(a);  // [7, 7, 7, 7, 7, 7, 7]
```

### As Function Arguments

Look at this code:

```import std.stdio;

{
arr += 1;
// this means arr = arr + 1
}

void main()
{
int array1 = [1, 2, 3];
writeln(array1); // [1, 2, 3]
}
```

The output is `[1,2,3]`, not `[2,2,3]`. Why? It's because static arrays are copied whenever they are passed as arguments to a function. The `arr` in the `addOne` function is only a copy of `array1`. Rewrite it like this:

```import std.stdio;

void addOne(ref int arr)
{
arr += 1;
}

void main()
{
int array1 = [1, 2, 3];