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

Lesson 7: Pointers, Pass-By-Reference and Static Arrays

edit

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

edit

Using Pointers

edit
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;
    add_numbers(&c, &d);
    writeln(c);  // 4
}

Pass By Reference

edit

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;
    add_numbers(c, d);
    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

edit
import std.stdio;

void main()
{
    int[5] a;
    a[0] = 1;
    writeln(a);  // [1, 0, 0, 0, 0]
    
    // a[10] = 3;  Error: Array index 10 is out of bounds
    
    int* ptr_a = &a[0];
    ptr_a[0] = 5;
    ptr_a[4] = 3;
    writeln(a); // [5, 0, 0, 0, 3]
    
    // ptr_a[10] = 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[3] b = 3;
    writeln(b);  // [3, 3, 3]
    b[] = 2;
    writeln(b);  // [2, 2, 2]
}

Concepts

edit

Reference and Dereference

edit

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

Pointer Types

edit

In D, a pointer to typeA is:

typeA*

and the code for declaring one is:

typeA* identifier;

.

Static Arrays

edit

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

byte[10] array_of_ten_bytes;

You can also do this:

byte[2] 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[7] 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

edit

Look at this code:

import std.stdio;

void addOne(int[3] arr)
{
    arr[0] += 1;
    // this means arr[0] = arr[0] + 1
}

void main()
{
    int[3] array1 = [1, 2, 3];
    addOne(array1);
    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[3] arr)
{
    arr[0] += 1;
}

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