How to create an array in D language
Overview
An array in D is a language struct that can contain multiple values that can be of same data type or different types as indicated during declaration.
The declaration of arrays in D is different from languages like python, PHP, and the likes.
Create an array in D
Arrays in D can be
These different types of arrays in D language are declared in different fashion and ways.
Code 1
Create a static array
Once we declare a static array, we can not increase the size along the way.
// import std.stdioimport std.stdio;// main methodvoid main() {// create as static arrayint [3] stats = [70,80,100];//try adding an extra fourth member, it throws an error.//uncomment the line below to see the error.// stats ~= 120;// print the length of the static arraywriteln(stats);}
Explanation
- Line 1 : We make the
importpackage. - Line 5: We start the
mainfunction. - Line 7: we declare a static array
statsand set its length as3. - Line 14: We print to the screen.
Code 2
Create a dynamic array
Once we declare a dynamic array, we can keep adding as many values as we wish, thereby increasing the size along the way.
// import std.stdioimport std.stdio;// main methodvoid main() {// create as dynamic arrayint [] carts = [70,80,100];writeln("length before adding new member: ",carts.length);//try adding an extra fourth member, it worked perfectly well.carts ~= 120;// print the length of the dyanmic arraywriteln("length now, after adding new member : ",carts.length);}
Explanation
-
Line 2: We make the
importpackage. -
Line 5: We start the
mainfunction. -
Line 7: We declare a dynamic array
cartsand set its length as3. -
Line 8: We print the length of the array at first to the screen.
-
Line 10: We add a new value to our dynamic array.
-
Line 13: We print the length of the array after a new value is added to screen .
Code 3
Create an associative array
As shown in the code below, associative arrays have keys, integers of our choice that point or serve as indexes to a value. To access these values we can use their key in place of an index.
// import std.stdioimport std.stdio;// main methodvoid main() {// create as dynamic arrayauto props = [70 :"shoes",80:"shirts", 100 : "electronics"];//try adding an extra fourth member, it worked perfectly well.writeln("Using key 70 to get value : ",props[70]);writeln("Using key 80 to get value : ",props[80]);writeln("Using key 100 to get value : ",props[100]);}
Explanation
-
Line 2: We make the
importpackage. -
Line 5: We start the
mainfunction. -
Line 7: We declare an associative array
propsand set its keys and values. -
Line 10, 11, and 12: We print the values of the associative array using its keys to screen .
Code 4
Create a pointer to data array
To create this array, we must have access to the values of another variable. This can be achieved using the dereference operator (*). We’ll also indicate the value to point, using the ampersand (&). Below is an example of how it works.
In the code below, the pointer to data type of array can be seen as an array that holds a pointer to arrays:
// import std.stdioimport std.stdio;// main methodvoid main() {// create an array to be pointedint [] arr = [70,80,100];//point to the new arrayint []* point = &arr;//print the pointer valuewriteln(point);//print arr using the pointerwriteln(point[0]);}
Explanation
-
Line 2: We make the
importpackage. -
Line 5: We start the
mainfunction. -
Line 7: We declare an array
arrand set its values. -
Line 9: We declare a pointer array to hold the pointer values of
arr. -
Line 11: We print the pointer value.
-
Line 13: We display the value of
arrusingp, its pointer array to locate it.
In this shot, we learned by examples how we could easily create each of the different types of arrays in D language.