# D (The Programming Language)/d2/Strings and Dynamic 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 |

# Lesson 8: Strings and Dynamic Arrays

In this chapter, you will learn about strings in depth. In the meantime, you will also learn about another type: the dynamic array.

## Concepts
### Dynamic Arrays
#### Dynamic Arrays versus Static Arrays

In the previous lesson, you learned about static arrays. Dynamic arrays are different from those in that they do not have a fixed length. Essentially, a dynamic array is a structure with this info:
* A pointer to the first element
* The length of the entire array
You create a dynamic array like this:
You can create a dynamic array initiated with a specific length:
You will learn more about the new keyword later. The syntax for filling an array with a single value is the same for both static and dynamic arrays:
Dynamic arrays are indexed in the same fashion as static arrays. The syntax for accessing an element at a certain index is the same:
However, since dynamic arrays don't have a length known at compile-time, the compiler can't check if the index that you are accessing is really within that length. Code like this would compile but would also cause a runtime Range Violation error:

#### Manipulation of Dynamic Arrays

Dynamic arrays can be combined with other dynamic arrays or even with static arrays using the ~ operator. Appending a new element to the array uses the same operator.
You shouldn't assign a dynamic array to a static array (my_static_arr = my_dynamic_arr;) unless if you are certain that my_dynamic_arr.length is the equal to my_static_arr.length. Otherwise, a runtime error will occur. You can always assign a static array to a dynamic array variable, though, since the dynamic array will automatically resize if the static array's length is too big.

#### Passing Dynamic Arrays to Functions

Dynamic Arrays are passed by value to functions. That means, when you pass a dynamic array to a function, the structure that contains the pointer to the first element and the length is copied and passed.
You learned in the last chapter that you can cause things to be passed by reference instead of by value by adding the ref modifier.

#### Array and Other Properties

These properties apply to both static and dynamic arrays:
| Property | Description |
| .init | For static arrays, it returns an array with each element initialized to its default value. |
| .sizeof | Returns the size of the array in memory. |
| .length | Returns the number of elements in the array. This is fixed in static arrays. |
| .ptr | Returns a pointer to the first element of the array. |
| .dup | Creates a dynamic array that is a copy of the array and returns it. |
| .idup | Similar to .dup but the copy's elements are immutable. |
| .reverse | Returns the array with its elements in reverse order. |
| .sort | Sorts in place the elements in the array and returns the result. |
There are also other properties which are common to every object or expression. Two of such properties are the .init and .sizeof properties.

### Immutable

In D, immutable is a storage-class just like auto. It converts a type to a non-modifiable type.
Note that immutable(type) means the same as immutable type to the compiler.

#### Storage-Classes and auto

When you have a storage class like immutable, you can omit auto for type inference.
The type of whatever is immutable is inferred from the compiler. This next code example is not valid because there is no way the compiler could infer the type:

#### Using immutable Variables

This is allowed and perfectly fine code:
It only sets b equal to the value of a. b does not have to be immutable. That changes if you are taking a reference:
You are allowed to cast the immutable away, but if you were to modify an immutable value using that hack, the result is undefined.

### Strings as Arrays

You've seen strings since Lesson 1. A string is the exact same thing as immutable(char), a dynamic array of immutable char elements. Likewise, a wstring is the same as immutable(wchar), and a dstring is the same as immutable(dchar).

#### String Properties

Strings have the same built-in properties as dynamic arrays. One useful property is the .dup property, for creating a mutable char copy of a string if you want to modify the individual characters of the string.
The .idup property is useful for creating a copy of an existing string, or for creating a string copy of a char.

#### Char Literals

A char literal is enclosed by single-quotes. There are also wchar and dchar literals.