Search⌘ K
AI Features

Array Initialization and Basic Array Operations

Explore how to initialize arrays with default or specified values and perform essential operations such as adding elements to dynamic arrays, removing items, sorting, reversing, and combining arrays. This lesson builds your foundation in handling arrays effectively in D programming.

Initializing the elements

Like every variable in D, the elements of arrays are automatically initialized. The initial value of the elements depends on the type of the elements: 0 for int, double.nan for double, etc.
All of the elements of the values array above are initialized to double.nan:

double[5] values; // elements are all double.nan

Obviously, the values of the elements can be changed later during the execution of the program. We have already seen that in the previous lesson when assigning to an element of an array:

monthDays[11] = 31;

That also happened when reading a value from the input:

readf(" %s", &values[counter]);

Sometimes the ...